none
如何用PHP获取通过BackgroundUploader上传的文件 RRS feed

  • 问题

  • 我按照这个页面的示例:http://msdn.microsoft.com/zh-cn/library/windows/apps/hh700372.aspx

    实现了选择图片并上传的操作,但是在PHP中怎么获取不到文件?$_FILES 是空的。

                function UploadOp() {
                    var upload = null;
                    var promise = null;
    
                    this.start = function (uriString, file) {
                        try {
    
                            var uri = new Windows.Foundation.Uri(uriString);
                            var uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader();
    
                            // Set a header, so the server can save the file (this is specific to the sample server).
                            uploader.setRequestHeader("Filename", file.name);
    
                            // Create a new upload operation.
                            upload = uploader.createUpload(uri, file);
    
                            // Start the upload and persist the promise to be able to cancel the upload.
                            promise = upload.startAsync().then(complete, error, progress);
                        } catch (err) {
                            $.dialog(err);
                        }
                    };
                    // On application activation, reassign callbacks for a upload
                    // operation persisted from previous application state.
                    this.load = function (loadedUpload) {
                        try {
                            upload = loadedUpload;
                            promise = upload.attachAsync().then(complete, error, progress);
                        } catch (err) {
                            $.dialog(err);
                        }
                    };
                };
    
                document.getElementById("upload_avatar").addEventListener("click", function () {
                    var picker = new Windows.Storage.Pickers.FileOpenPicker();
                    picker.fileTypeFilter.replaceAll([".jpg", ".bmp", ".gif", ".png"]);
                    picker.pickSingleFileAsync().then(function (file) {
                        if (file) {
    
                            var upload = new UploadOp();
                            upload.start("demo.php", file);
                        };
                    }, function (error) {
                        //error
                    });
                }, false);


    SkyShow


    • 已编辑 小影 2014年5月6日 9:02
    2014年5月6日 9:01

答案

  • 你好 小影,

    你有没有尝试在then()里面加入一个complete的方法来保证上传已经成功了。另外如果上传的对象是图片的话,可能还需要把他转化为Blob格式,图片是没办法直接上传的。

    下面是之前写过的类似图片上传代码,不过通过的xhr来实现,感觉如果图片不是很大的话,完全可以通过xhr来实现啊

    openPicker.pickSingleFileAsync().then(function (file) {
      file.openAsync(Windows.Storage.FileAccessMode.read).done(function (stream) {
          var blob = MSApp.createBlobFromRandomAccessStream("image/jpg", stream);
          var fdata = new FormData();
          fdata.append('upload',blob);
    
          WinJS.xhr({
              type: "post",
              url: "postImage.php",
              data: fdata,
              // headers: { "Content-type": "application/octet-stream" }
    
    
          }).done(function (result) {
              console.log(result.responseText)
          });
      });
    });
    

    希望能帮到你。

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • 已标记为答案 小影 2014年5月8日 7:51
    2014年5月7日 7:12
    版主