0

我在将图像上传到服务器时遇到问题。我使用winjs xhr post将图像上传到服务器

问题是 php 中的 upload_field index not defined 错误请在下面找到用于从 Windows 8 上传图像的代码库。

function pickImage() {
// Create the picker object and set options
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
// Users expect to have a filtered view of their folders depending on the scenario.
// For example, when choosing a documents folder, restrict the filetypes to documents for your application.
openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);

// Open the picker for the user to pick a file
openPicker.pickSingleFileAsync().then(function (file) {
    if (file) {
        // Application now has read/write access to the picked file
        WinJS.log && WinJS.log("Picked photo: " + file.name, "sample", "status");
var fdata = new FormData();
fdata.append('upload_field',file);

       WinJS.xhr({
            type: "post",
            url: "http://localhost/imgupload.php",
            data: fdata,
            // headers: { "Content-type": "application/octet-stream" }


        }).done(function (result) {
            console.log(result.responseText)
        });

        document.getElementById('imgCapture').src
    = window.URL.createObjectURL(file);
    } else {
        // The picker was dismissed with no selected file
        WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
    }
});

}

下面是我的 php 代码库。

$filesize = filesize($_FILES['upload_field']['tmp_name']); 
echo "The uploaded file size is " . $filesize . " bytes\n";
echo "Stored in: " . $_FILES["upload_field"]["tmp_name"];
$timestamp = time();

if (file_exists("upload/" . $_FILES["upload_field"]["name"]))
  {
  echo $_FILES["upload_field"]["tmp_name"] . " already exists. ";
  }
else
  {   
  $fname = $timestamp.$_FILES["upload_field"]["name"];
  move_uploaded_file($_FILES["upload_field"]["tmp_name"],
  "upload/" . $fname);
  echo "Stored in: " . "upload/" .$fname ;
  }
 echo "image uploaded";

请帮助我 - 感谢您的帮助。!!!

4

1 回答 1

2

您不应该直接使用文件对象。您必须打开该文件并使用它创建一个 Blob。试试这样:

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_field',blob);

      WinJS.xhr({
          type: "post",
          url: "http://localhost/imgupload.php",
          data: fdata,
          // headers: { "Content-type": "application/octet-stream" }


      }).done(function (result) {
          console.log(result.responseText)
      });
  });
});
于 2013-03-22T20:20:33.930 回答