【问题标题】:How do I retrieve image properly using ParseFile?如何使用 ParseFile 正确检索图像?
【发布时间】:2015-02-17 18:46:54
【问题描述】:

我正在使用以下代码来保存图像

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 标签显示图像,但图像总是损坏。

【问题讨论】:

标签: c# .net asp.net-mvc parse-platform


【解决方案1】:

您的示例图像包含 762Kb 的零字节。在您的保存方法中我也看不到您实际上将内容放入数组中。

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

上面的代码创建了一个 x 字节长的空数组,然后你保存它。您应该以某种方式将图片字节放在那里。我无法从您的代码中判断 ProfileImage 对象是什么类型,但我想您可以从该对象中获取内容字节。

【讨论】:

  • ProfileImage 是 HttpPostedFileBase。它来自提交到表单的 html
  • 查看@stackoverflow.com/questions/7852102/… 了解更多信息。该线程给出了一个从发布的文件中获取字节的示例。
  • 宾果游戏!非常感谢。做到了。
猜你喜欢
  • 2016-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 1970-01-01
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多