0

サーバーに画像をアップロードする際に問題に直面しています。私はwinjs xhr postを使用して画像をサーバーにアップロードします

問題はphpでupload_fieldインデックスが定義されていないというエラーです.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 に答える