2014-12-18 17 views
1

我使用下面的代碼保存如何使用ParseFile正確檢索圖像?

ParseFile file = null; 

if (ProfileImage != null && ProfileImage.ContentLength > 0 && !string.IsNullOrEmpty(ProfileImage.FileName)) 
{ 
    byte[] fileBytes = new byte[ProfileImage.ContentLength]; 
    file = new ParseFile(ProfileImage.FileName, fileBytes); 
    await file.SaveAsync(); 

var player = new ParseObject("Player"); 
if(file != null) 
{ 
    player["ProfilePic"] = file; 
} 

await player.SaveAsync(); 
} 

我試圖檢索圖片,因此我可以顯示使用下面的代碼

ParseObject player = Task.Run(() => 
    ParseObject.GetQuery("Player").WhereEqualTo("objectId", id).FirstOrDefaultAsync()).Result; 

    ParseFile profileImage = null; 
    string profileImageUrl = ""; 
    if (player.ContainsKey("ProfilePic")) 
      { 
       profileImage = player.Get<ParseFile>("ProfilePic"); 
       profileImageUrl = profileImage.Url.AbsoluteUri; 
      } 

profileImageUrl有一個正確的URL,如圖像http://files.parsetfss.com/c8db8833-b04b-4877-8040-b70df1ec216c/tfss-11c442f3-6146-4316-a6c0-7c9a063d897e-Koala.jpg

但是,圖像始終被破壞。我試圖用html標籤來顯示圖像,並且圖像總是被打破。

+0

與論壇網站不同,我們不使用「謝謝」或「任何幫助表示讚賞」,或在[so]上簽名。請參閱「[應該'嗨','謝謝',標語和致敬從帖子中刪除?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be –

回答

2

您的示例圖像包含762Kb的零字節。在保存方法中,我無法真正看到實際上是將內容放入數組中。

byte[] fileBytes = new byte[ProfileImage.ContentLength]; 
file = new ParseFile(ProfileImage.FileName, fileBytes); 
await file.SaveAsync(); 

上面的代碼創建一個x字節長的空數組,然後保存它。你應該把圖片字節放在那裏。我無法從代碼中知道ProfileImage對象是什麼類型,但是我想你可以從該對象中獲取內容字節。

+0

ProfileImage是HttpPostedFileBase。它來自提交給表單 – atbebtg

+0

的html Look @ http://stackoverflow.com/questions/7852102/convert-httppostedfilebase-這個線程給出了一個例子來獲取來自發布文件的字節 –

+0

Bingo!非常感謝你,它做到了。 – atbebtg

相關問題