我正在使用Cordova(CLI)构建一个应用程序,以将文件及其所在位置上传到服务器。到目前为止,唯一不起作用的是将文件上传到服务器。文件上传似乎工作正常,但POST数据永远不会出现在服务器上。
以下是用于上传的代码:
function uploadToServer () {
bootstrap_alert("Uploading!","Please do not close PowerSnap.", "info") // Could bootstrap be causing any trouble?
$('#uploadButton').button('loading'); //Sets the upload button to loading state
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = 'file.jpg';
options.mimeType= "image/jpeg";
options.headers={'Connection':'close'};
var params = {};
params.command = "upload";
params.coordinates = locationTextBox.value;
options.params = params;
uriToUpload = uploadFileEntry.toURL();
var ft;
ft = new FileTransfer();
ft.upload(uriToUpload, encodeURI('http://url.removed.com'), uploadSuccess, uploadError, options, true); // The PHP script is named index.php so the server knows to redirect to it.
}
logcat似乎表明它有效。
D/FileTransfer( 8014): upload cdvfile://localhost/temporary/1393131949448.jpg to http://powersnap.7katz.com
D/FileTransfer( 8014): fileKey: file
D/FileTransfer( 8014): fileName: file.jpg
D/FileTransfer( 8014): mimeType: image/jpeg
D/FileTransfer( 8014): params: {"command":"upload","coordinates":"(Omitted, but functioning properly"}
D/FileTransfer( 8014): trustEveryone: true
D/FileTransfer( 8014): chunkedMode: true
D/FileTransfer( 8014): headers: {"Connection":"close"}
D/FileTransfer( 8014): objectId: 1
D/FileTransfer( 8014): httpMethod: POST
D/FileTransfer( 8014): Content Length: 2486171
D/FileTransfer( 8014): Uploaded 114958 of 2486171 bytes
D/FileTransfer( 8014): Uploaded 229646 of 2486171 bytes
D/FileTransfer( 8014): Uploaded 344334 of 2486171 bytes
D/FileTransfer( 8014): Uploaded 459022 of 2486171 bytes
D/FileTransfer( 8014): Uploaded 573710 of 2486171 bytes
这会持续几秒钟,然后最终会到达:
D/FileTransfer( 8014): Sent 2486171 of 2486171
D/FileTransfer( 8014): response code: 200
D/FileTransfer( 8014): response headers: {null=[HTTP/1.1 200 OK], Connection=[close], Content-Type=[application/json], Date=[Sun, 23 Feb 2014 05:06:17 GMT], OkHttp-Received-Millis=[1393131977469], OkHttp-Response-Source=[NETWORK 200], OkHttp-Selected-Transport=[http/1.1], OkHttp-Sent-Millis=[1393131977136], Server=[Apache], Transfer-Encoding=[chunked]}
D/FileTransfer( 8014): got response from server
D/FileTransfer( 8014): Error
最后一行(D/FileTransfer( 8014): Error
)是PHP切换后来自服务器的响应,表明服务器没有得到参数。 (服务器上的access.log显示应用已连接)当我让服务器将$_FILES
或$_POST
记录到文件时,如果该文件不存在且为空,则创建该文件。我已经使用Advanced Rest Client for Chrome测试了接收文件的PHP脚本,并且脚本按预期工作。感谢您的帮助,如果这个问题不明确,请抱歉。