【发布时间】:2013-10-09 14:44:44
【问题描述】:
我自己回答了这个问题,因为我花了很长时间才找到解决方案,而且没有很好的记录。
【问题讨论】:
我自己回答了这个问题,因为我花了很长时间才找到解决方案,而且没有很好的记录。
【问题讨论】:
在尝试使用 FileTransfer() 将图像从 Android 上的 phonegap 应用程序上传到远程服务器时,我不断在每个备用文件上收到 错误代码 3上传。
它工作过一次,但当我再次尝试时它会立即抛出错误,甚至不会将文件发送到服务器。
我用于文件上传的代码是:
使它起作用的关键是添加一个标题选项。
options.headers = {
Connection: "close"
}
options.chunkedMode = false;
完整代码:
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
options.chunkedMode = false;
*options.headers = {
Connection: "close"
};*
// setup parameters
var params = {};
params.fullpath =imageURI;
params.name = options.fileName;
var ft = new FileTransfer();
ft.upload(imageURI, encodeURI(url+'/account/profile-pics'), win, fail, options);
function win(r) {
//file uploaded successfully
}
function fail(error) {
alert("An error has occurred: Code = " + error.code);
alert("upload error source " + error.source);
alert("upload error target " + error.target);
}
【讨论】:
错误代码 3 是一个相当广泛的错误;这基本上意味着您的服务器编码不正确或您没有互联网连接,这会导致连接错误。
可能的意思:
upload_max_filesize 设置,要在 ExpressJS 上解决此问题,您需要调整 Multer 的 limit 字段等。基本上,增加服务器上的文件上传大小。大多数服务器限制文件上传大小作为安全措施。 (https://www.owasp.org/index.php/Unrestricted_File_Upload)options.fileKey 值(即<input type="file" name="fileKey" />)不是您的服务器期望的名称 - 示例错误消息可能是“意外字段”。content-type 字段没有multipart/form-data; boundary=----WebKitFormBoundary 的值。在服务器上记录请求标头,可用于检查内容类型是否设置正确。@AugieGardner - 同样同意 Cordova 文件传输插件没有很好地记录上传使用相机插件拍摄的照片。
幸运的是,我有一个适用于 iOS 的示例(我猜 Android 也是如此):
cordova file transfer plugin not working in ios simulator
一个更简单的替代方法(或备用)是将图像编码为 Base64,并通过普通的旧 AJAX POST 请求发送它。其中包括以下优点和缺点。
通过 AJAX 发送 Base64 编码图像的缺点
通过 AJAX 发送 Base64 编码图像的优势
【讨论】: