我面临将图片上传到服务器的问题。 即时通讯使用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";
请帮助我 - 感谢您的帮助。!!!
答案 0 :(得分: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)
});
});
});