【问题标题】:Windows store apps JS : uploading a video file with WinJS.xhrWindows 商店应用 JS:使用 WinJS.xhr 上传视频文件
【发布时间】:2015-01-12 14:01:20
【问题描述】:

我在将视频上传到表单时遇到了一些问题。 就我而言,我需要在我的视频中上传一些数据,所以我离开 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(file))

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;}

有人知道如何使它工作吗?或者也许我做错了,这不是我想转换我的文件的方式(这是处理图像的方式,也许不是视频)

【问题讨论】:

    标签: javascript windows-store-apps winjs


    【解决方案1】:

    好的,我找到了一种方法。首先,您需要使用 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");
        });
    

    【讨论】:

      猜你喜欢
      • 2013-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 2016-05-04
      • 2015-07-28
      相关资源
      最近更新 更多