Windows 商店应用 JS:使用 WinJS.xhr 上传视频文件

Windows store apps JS : uploading a video file with WinJS.xhr

我在将视频上传到表单时遇到了一些问题。 在我的例子中,我需要用我的视频上传一些数据,所以我离开了 BackgroundUploader 使用 WinJS.xhr。但我不知道如何将我的视频文件转换成我的 php.

可读的文件

我的代码:

    var clickPicker = function () {

    openPicker = Windows.Storage.Pickers.FileOpenPicker();

    // We set the default location to the video library 
    openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.videosLibrary;
    // Set de view to thumbnail 
    openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;

    // Extension allowed to be taken
    openPicker.fileTypeFilter.replaceAll([".mp4", ".avi"]);

    openPicker.pickSingleFileAsync().done(function (file) {

        uploadInit(file);

    }, function (err) {

        // MISTAAAAAAAAAAAAAAAAKEEEEEEEEE
        console.log(err.message);

    });

};

var uploadInit = function (file) {

    // Creating the blob
    var objectURL = window.URL.createObjectURL(file);

    var url = "http://localhost/vdm_bo/videos/uploader";

    var d = new Date();

    var data = new FormData();
    data.append("data[Video][pseudo]", 'H4mm3R');
    data.append('data[Video][postal_code]', '67340');
    // Converting date to a datetile mysql
    data.append('data[Video][date]', ISODateString(d));
    data.append('data[Video][age]', '24');
    data.append("data[Video][email]", 'bliblu@hotmail.fr');
    data.append("data[Video][question_selected]", 'qA');
    data.append("data[Video][video_file]", file, file.name);

    WinJS.xhr({
        type: "POST",
        url: url,
        data: data
    }).done(function (res) {

        console.log('succes');

    }, function (err) {

        console.log(err.message);

    }, function (res) {

    });

};

所以,为了调试这个,我序列化了答案,这是我得到的: 上传文件时(无 blob):

s:36:"[object Windows.Storage.StorageFile]";

使用 blob 上传时 (window.URL.createObjectURL(文件))

s:41:"blob:9A06AB11-8609-42DC-B0A9-7FB416E70A9D";

当我使用 html 表单上传视频时

a:5:{s:4:"name";s:36:"9147cb17e216d5182908ad370ff16914.mp4";s:4:"type";s:9:"video/mp4";s:8:"tmp_name";s:23:"C:\wamp\tmp\php13C8.tmp";s:5:"error";i:0;s:4:"size";i:26454182;}

有人知道如何让它发挥作用吗?或者也许我做错了,这不是我想转换文件的方式(这是处理图像的方式,可能不适用于视频)

好的,我找到了一种方法。首先,您需要使用 getFileAsync() 而不是 Picker 获取文件。然后你可以用你的文件流创建一个 blob,并将这个 blob 添加到你的表单中。 这是我的代码

        var videosLibrary = Windows.Storage.KnownFolders.videosLibrary;

    videosLibrary.getFileAsync(file.name).then(
        function completeFile(file) {
            return file.openAsync(Windows.Storage.FileAccessMode.readWrite);
        }).then(
            function completeStream(stream) {

                var d = new Date();
                // Do processing. 
                var blob = MSApp.createBlobFromRandomAccessStream("video/mp4", stream);

                var data = new FormData();
                data.append("data[Video][pseudo]", 'H4mm3R');
                data.append('data[Video][postal_code]', '67340');
                // Converting date to a datetile mysql
                data.append('data[Video][date]', ISODateString(d));
                data.append('data[Video][age]', '24');
                data.append("data[Video][email]", 'bliblu@hotmail.fr');
                data.append("data[Video][question_selected]", 'qA');
                data.append("data[Video][video_file]", blob, file.name);

                return WinJS.xhr({ type: "POST", url: "http://localhost/vdm_bo/videos/uploader", data: data });
        }).then(
            function (request) {
                console.log("uploaded file");
            }, 
            function (error) {
               console.log("error uploading file");
    });