gpt4 book ai didi

php - Cordova XMLHttpRequest PHP 上传

转载 作者:数据小太阳 更新时间:2023-10-29 02:46:57 25 4
gpt4 key购买 nike

我正在使用 Cordova,并尝试通过 PHP 脚本将音频文​​件上传到我的服务器。

我正在处理这个例子

https://cordova.apache.org/blog/2017/10/18/from-filetransfer-to-xhr2.html

示例代码建议将读入文件对象作为 blob 传递。

reader.onloadend = function() {
// Create a blob based on the FileReader "result", which we asked to be retrieved as an ArrayBuffer
var blob = new Blob([new Uint8Array(this.result)], { type: "image/png" });
var oReq = new XMLHttpRequest();
oReq.open("POST", "http://mysweeturl.com/upload_handler", true);
oReq.onload = function (oEvent) {
// all done!
};
// Pass the blob in to XHR's send method
oReq.send(blob);
};

我已修改代码以将其附加为数据对象,因为未创建文件

window.resolveLocalFileSystemURL(fileSystem, function (dir) {

dir.getFile(fileName, {create: true}, function (fileEntry) {

fileEntry.file(function (file) {

var reader = new FileReader();

reader.onloadend = function() {

var data = new FormData();

var oReq = new XMLHttpRequest();
oReq.open("POST", "http://mywebsite.com/up.php", true);

var blob = new Blob([new Uint8Array(this.result)], { type: "audio/mpeg" });
data.append('fileToUpload', blob );

oReq.onload = function (oEvent) {
// all done!

};
// Pass the blob in to XHR's send method
oReq.send(data);

};

// Read the file as an ArrayBuffer
reader.readAsArrayBuffer(file);

}

...

我的 php

$target_dir = $_SERVER['DOCUMENT_ROOT'] . "/uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
//echo "uploaded";
} else {
//echo "error";
}
  • 我已确认我有读/写权限。
  • 我已确认我可以使用标准 html 表单和上述 php 脚本上传测试音频文件。
  • 我检查了我的虚拟主机错误日志和 Apache 系统日志,没有发现任何错误。我可以看到正在进行的 POST 但没有错误消息。
  • 我已经确认 Cordova 获得的文件对象是准确的并且确实存在。
  • 我试过几种不同的 mime 类型都没有效果

任何帮助将不胜感激。

谢谢

最佳答案

试试这个。

<?php
print_r($_FILES);
$new_image_name = "YEAH.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/TEST/".$new_image_name);
?>

js代码

<script type="text/javascript" charset="utf-8">

// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap is ready
function onDeviceReady() {
console.log("device ready");
// Do cool things here...
}

function getImage() {
// Retrieve image file location from specified source
navigator.camera.getPicture(uploadPhoto, function(message) {
alert('get picture failed');
},{
quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
}
);

}

function uploadPhoto(imageURI) {
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";

var params = new Object();
params.value1 = "test";
params.value2 = "param";

options.params = params;
options.chunkedMode = false;

var ft = new FileTransfer();
ft.upload(imageURI, "http://some.server.com/TEST/upload.php", win, fail, options);
}

function win(r) {
console.log("Code = " + r.responseCode.toString()+"\n");
console.log("Response = " + r.response.toString()+"\n");
console.log("Sent = " + r.bytesSent.toString()+"\n");
alert("Code Slayer!!!");
}

function fail(error) {
alert("An error has occurred: Code = " + error.code);
}

</script>

关于php - Cordova XMLHttpRequest PHP 上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56105475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com