How do I encode a file from the filesystem as multipart / form-data?

I want users to upload photos to Facebook in my image viewer app. As you can see in this article, Facebook GUI API - Upload photo using JavaScript , I need to encode my photos as multipart / form-data in order to be able to upload. How do I archive this encoding in Windows.Storage.StorageFile

elements?

0


source to share


1 answer


You need to open this photo (type Windows.Storage.StorageFile

) for reading, convert it to blob, add it to an object FormData

and load it using any necessary Ajax library ( WinJS.xhr

, jQuery.ajax

etc.) ..

The following code illustrates this 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
});

      

+1


source







All Articles