c# - 将图像作为解析文件上传

标签 c# wpf parse-platform

我正在尝试从 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/

相关文章:

c# - 在第一次尝试使用 WCF 失败后,客户端如何尝试重新连接到服务器?

c# - 避免将 “http://www.w3.org/2001/XMLSchema-instance” 命名空间与 .Net DataContractSerializer 一起使用

c# - XAML - 为什么数据绑定(bind) TwoWay 不适用于 .net 4.0 中组合框的文本属性?

javascript - Parse JS ACL - Parse.User.current() 的 getPublicReadAccess 总是返回 false?

elasticsearch - 使用 ElasticSearch 托管 : Parse. com

ios - UIImage数据方法返回nil

C# 内联 lambda 评估

wpf - 有没有办法设置 WPF DataGrid NewItem 占位符的样式

c# - Windows 8 - 花哨的进度条 API?

c# - NHibernate:在同一对象上具有与映射属性相同的非映射属性