在 cordova 项目中,我正在使用文件传输插件上传个人资料照片,它总是返回代码 1 错误
In cordova project I am using file transfer plugin to upload a profile photo it always returns Code 1 error?
我正在尝试使用cordova文件传输插件上传照片。但总是返回代码 1 错误,这是FILE_NOT_FOUND错误,但相机插件返回 imageURI 完全正常。我已经在邮递员那里测试了我的代码,它工作正常。但是在 javascript 中,我收到代码 1 错误。我也尝试对图像 URI 进行硬编码,但错误保持不变。谁能告诉我有什么问题?这是我正在做的事情的片段:-
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
mediaType: Camera.MediaType.PICTURE,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY
});
function onPhotoURISuccess(imageURI) {
alert(imageURI);
var options = new FileUploadOptions();
options.fileKey="file";
//options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.fileName="image.jpeg";
options.mimeType="image/jpeg";
options.httpMethod="PUT";
options.trustAllHosts=true;
options.chunkedMode=false;
var header ={};
//header.ContentType="image/jpeg";
header.Connection="close";
//header.Authorization="Basic c2FoaWwuc2V0aGk6V2VsY29tZUAwNw==";
options.headers =header;
var ft = new FileTransfer();
ft.upload(imageURI,encodeURI("servername.com"/profiles/photo.do?
key="hrhgfhjf23435"),win,fail,options);
function win(r) {
alert("file uploaded");
console.log("Code = " + r.responseCode);
}
function fail(error) {
alert("An error has occurred: Code = " + error.code);
alert("upload error source " + error.source);
alert("upload error target " + error.target);
}
}
function onFail(message) {
//Uncomment to view the image file URI
alert("in fail");
alert(message);
}
在实现时,它返回代码 1 错误。我已经在邮递员中运行了这个来检查 URL,但在邮递员中它工作正常。
imageURI大概是"file:///data/data/..."-因此问题可能是FileTransfer无法访问该文件夹。
在尝试将照片从照片库上传到社交媒体时,我遇到了类似的问题。所以我将文件复制到应用程序的缓存并从那里上传图片:
moveFile($scope.imageURI);
function moveFile(fileUri) {
window.resolveLocalFileSystemURL(
fileUri,
function(fileEntry){
window.resolveLocalFileSystemURL(
cordova.file.externalCacheDirectory,
function(dirEntry) {
fileEntry.copyTo(dirEntry, fileEntry.name, onSuccess(fileEntry, dirEntry), onFail);
},
onFail);
},
onFail);
}
function onSuccess(fileEntry, dirEntry) {
$scope.imageURI = dirEntry.nativeURL + fileEntry.name;
}
function onFail() {
alert("error copying picture");
}
试试这段代码。它对我有用。您只需在单击按钮时调用getImage函数,或者在您想要调用的任何地方调用DeviceReady。
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();
//YOUR_URL = Actual web url where web-service is exists.
ft.upload(imageURI, "YOUR_URL", win, fail, options);
}
function win(r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
alert(r.response);
}
function fail(error) {
alert("An error has occurred: Code = " + error.code);
}
这是我的PHP网络服务,它将我的文件存储到服务器。
<?php
print_r($_FILES);
$new_image_name = "namethisimage.png";
move_uploaded_file($_FILES["file"]["tmp_name"], "YOUR_SERVER_URL".$new_image_name);
?>
相关文章:
- 使用Facebook登录时未填写个人资料
- 显示LinkedIn Api的个人资料数据,无需身份验证
- AngularJS+Firebase上传个人资料图片
- 护照个人资料 ID
- 显示脸书好友个人资料图片时出现问题
- 如何在ionic中弹出个人资料图片
- 如何查找任何给定Facebook用户的LinkedIn个人资料
- Javascript-使用Facebook登录,显示用户's成功登录后的个人资料图片
- Javascript-获取一张用户的随机个人资料图片'的朋友在脸书上
- 护照个人资料未通过新属性
- 推特小部件_bigger个人资料图片
- 如何使用JavaScript SDK更改Facebook个人资料图片并按时更改其背面
- 在 cordova 项目中,我正在使用文件传输插件上传个人资料照片,它总是返回代码 1 错误
- Facebook共享仅对我的个人资料JavaScript API可见
- 钛加载Facebook个人资料图片集成不起作用
- 使用Selinium,Scrapy,Python检索用户个人资料的公共Facebook墙帖子
- 获取使用帐户/个人资料照片OneDrive/Microsoft API
- 如何通过JavaScript SDK从Facebook获取封面照片和个人资料照片
- 使用ONSEN UI + Angular上传个人资料照片
- 在AngularJS中嵌入我个人资料中的Instagram近期照片