我正在尝试从 WPF 应用程序中上传选定的图像文件以存储在 Parse 上,但是我无法在任何地方找到执行此操作的正确方法。
目前,我已从“OpenFileDialog”中选择了图像,并将该图像的路径存储在文本框中。
我现在如何将此文件上传到 Parse?
我熟悉解析,并且在 Objective-C 中保存字符串、图像、视频等没有任何问题,但我一生都无法想到如何让它在 C# 的 WPF 应用程序中工作。
任何帮助将不胜感激。
最佳答案
这是一段加载图像文件并将数据保存到字节数组中的代码。
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...
}
关于c# - 将图像作为解析文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27469183/