作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在使用 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";
}
任何帮助将不胜感激。
谢谢
最佳答案
试试这个。
<?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/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!