【发布时间】:2019-05-21 16:52:50
【问题描述】:
我正在使用 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 脚本上传测试音频文件。
- 我检查了我的 vhost 错误日志和 Apache 系统日志,没有发现任何错误。我可以看到 POST 正在进行,但没有错误消息。
- 我已经确认 Cordova 获得的文件对象是准确的并且确实存在。
- 我尝试了几种不同的 mime 类型,但没有任何效果
任何帮助将不胜感激。
谢谢
【问题讨论】:
标签: php xml cordova xmlhttprequest phonegap