如何将文件系统中的文件编码为multipart/form-data? [英] How to encode a file from the file system as multipart/form-data?
问题描述
我想让用户在我的图像查看器应用程序中将照片上传到Facebook.如本文所见, Facebook Graph API-使用JavaScript上传照片 ,我必须将我的照片编码为multipart/form-data才能上传.如何在Windows.Storage.StorageFile
项目上存档此编码?
I want to let users upload photos to Facebook in my image viewer app. As seen in this post, Facebook Graph API - upload photo using JavaScript, I have to encode my photos as multipart/form-data to be able to upload them. How to archive this encoding on Windows.Storage.StorageFile
items?
推荐答案
您需要打开该照片(类型为Windows.Storage.StorageFile
)进行阅读,将其流转换为blob,将其附加到FormData
对象中,然后使用任何方式上传您想要的Ajax库(WinJS.xhr
,jQuery.ajax
等).
You need to open that photo (of type Windows.Storage.StorageFile
) for reading, convert it's stream to blob, append it to FormData
object and upload using whatever Ajax library you want (WinJS.xhr
, jQuery.ajax
etc).
以下代码比单词更能说明这一点:
Following code illustrates it better than words:
file.openReadAsync().done(function(fileStream) {
var fileData = MSApp.createBlobFromRandomAccessStream(file.contentType, fileStream);
var formData = new FormData();
formData.append('upload', fileData, file.name);
... // send formData as xhr request body
});
这篇关于如何将文件系统中的文件编码为multipart/form-data?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!