将图像上传为Parse文件

时间:2014-12-14 12:16:29

标签: c# wpf parse-platform

我正在尝试从我的WPF应用程序中上传一个选择的图像文件以存储在Parse上但是我无法在任何地方找到正确的方法。

目前我从'OpenFileDialog'中选择了我的图像,并将该图像的路径存储在文本框中。

我现在如何将此文件上传到Parse?

我熟悉parse并且在Objective-C中保存字符串,图像,视频等没有问题但是在我的生活中不能想到如何在C#中的WPF应用程序中使用它。

任何帮助都会受到大力赞赏。

1 个答案:

答案 0 :(得分:2)

这是一段加载图像文件并将数据保存到字节数组中的代码。

private byte[] LoadByteArrayFromFile(string fileName)
{
    try
    {
        using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
        {
            byte[] byteArray = new byte[fs.Length];
            int bytesRead = 0;
            int bytesToRead = (int)fs.Length;

            while (bytesToRead > 0)
            {
                int read = file.Read(byteArray, bytesRead, bytesToRead);
                if (read == 0)
                    break;
                bytesToRead -= read;
                bytesRead += read;
            }

            return byteArray;
        }
    }
    catch (Exception ex)
    {
        return null;
    }
}

所以你先得到数据。

byte[] data = LoadByteArrayFromFile(filename); //OpenFileDialog.Path, full path to the image

然后,构建a ParseFile - 您应该熟悉其余步骤。

if (data != null)
{
    ParseFile file = new ParseFile(System.IO.Path.GetFileName(filename), data); 
    await file.SaveAsync();
    //then assign the ParseFile into a ParseObject, like the doc says...
}
相关问题