【问题标题】:Use WinJS.xhr to PUT a file to a rest service使用 WinJS.xhr 将文件放入休息服务
【发布时间】:2013-05-20 13:13:21
【问题描述】:

真正简单的任务。我有一个指向 Win8 客户端应用程序中的文件的 URL,我是通过执行以下操作获得的:

this.image = URL.createObjectURL(file);    

现在我需要像这样通过互联网发送文件:

var options = {
type: method,
url: MyApplication.servicesUri + "rest/" + dpName + queryParameters,
headers: { "Content-type": "application/json" },
data: JSON.stringify(objParameters)
};
WinJS.xhr(options)

所以问题是,我如何“反转”我的第一个操作并获取文件,以便我可以将其设置为我的 PUT 请求的data

【问题讨论】:

    标签: javascript windows-8 winjs


    【解决方案1】:

    我在这里寻找从 URL 中取回 StorageFile 的方法,该 URL 是由调用 URL.CreateObjectURL(storageFile) 创建的。

    我也在social.msdn 上看到了 Sebastian 的帖子。无法从 URL 取回存储文件。我只需要使用 WinJS.xhr() 而不是后台传输。

    我最终使用了同时保留 storageFile 和 URL 引用的解决方案 - [URL 绑定到 image.src 用于显示和 storageFile 上传到 blob。 MSApp.CreateFileFromStorageFile 对此非常有用。完整代码sn-p可见here

    更新:我找到了一种从 URL.CreateObjectURL 返回的 URL 中获取文件对象的方法。这来自w3 File API spec 第 12.7 节。只需使用 WinJS.xhr 即可获取 blob。

    // url like - blob: <guid>
    WinJS.xhr({ url: url, responseType: 'blob' });
    }).then(function (req)
    {
        var blob = req.response;
    }).then(null, function onerror()
    {
    });
    

    【讨论】:

      【解决方案2】:

      我用不同的方法解决了这个问题,但是这个问题的答案来自DataRead。查看 msdn 页面上的示例。

      【讨论】:

        猜你喜欢
        • 2012-04-12
        • 1970-01-01
        • 1970-01-01
        • 2019-04-17
        • 1970-01-01
        • 2012-09-26
        • 1970-01-01
        • 1970-01-01
        • 2017-04-05
        相关资源
        最近更新 更多