3

我在结合 Cordova 的 camera.getPicture 插件和他们的 FileTransfer.upload 插件时遇到了一些问题。最奇怪的是,当我从相机拍摄照片时,一切都很好,而不是当我从图书馆检索照片时。

问题看起来像这样:Phonegap/Cordova Uploading Image from gallery is not working on Android Kitkat

但我使用的是 Cordova 3,所以必须解决 kitkat 问题。

我的代码:

var imageUploadUrl = setting_api_url + "Umbraco/Api/ImageApi/PostUpload";
var formSubmitUrl = setting_api_url + "Umbraco/Api/FormApi/PostSend";
var imageUriStorage = [];
var resultsCollection = [];
var FieldId;

var take_photo = function(fieldId) {
FieldId = fieldId;
navigator.camera.getPicture(onTakeSuccess, 
                            onTakeFail, 
                            { 
    quality: 30, 
    destinationType: Camera.DestinationType.FILE_URI, 
    sourceType: Camera.PictureSourceType.CAMERA,
    targetWidth: 800,
    targetHeight: 800,
    correctOrientation: true
});
};

var get_photo = function(fieldId) {
FieldId = fieldId;
navigator.camera.getPicture(onTakeSuccess, onTakeFail, { 
    quality: 30, 
    destinationType: Camera.DestinationType.FILE_URI, 
    sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
    targetWidth: 800,
    targetHeight: 800,
    correctOrientation: true });
};

var onTakeSuccess = function(imageURI) {
console.log("onTakeSuccess imageURI= " + imageURI);

var image = document.getElementById("preview-" + FieldId);

// UPLOAD PART COPIED
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = src.substr(src.lastIndexOf('/') + 1);
var filetype = src.substr(src.lastIndexOf('.') + 1);
if(filetype == "png") {
    options.mimeType = "image/png";
}
else if (filetype == "jpg") {
    options.mimeType = "image/jpeg";
}

var ft = new FileTransfer();
ft.upload(imageURI, encodeURI(imageUploadUrl), image_upload_win, image_upload_fail, options);
// END.


imageUriStorage.push({ 'Id':FieldId, 'Path': imageURI});

image.src = imageURI;
};

var onTakeFail = function(message) {
alert('on take fail. Code: ' + message);
};

// called when upload succeeds        
var image_upload_win = function (r) {
    console.log("Code = " + r.responseCode);
    console.log("Response = " + r.response);
    console.log("Sent = " + r.bytesSent);
};

// when upload fails
var image_upload_fail = function (error) {
    alert("Code = " + error.code + "-" + error.http_status + ", image:" + error.source);
    console.log("upload error source " + error.source);
    console.log("upload error target " + error.target);
};

take_photo 和 get_photo 都命中了相同的函数 onTakeSuccess,因此它们将获得相同的过程。当源是相机时,图像被正确上传,服务器给出正确的响应(和 HTTP 201)。

但是,当源是库时,这会失败。FileTransfer.Upload 给出错误代码 1(找不到文件),但这不可能是真的,因为图像在我的应用程序的图像元素中正确显示。

服务器给出 HTTP 400 错误请求。

这个过程有一个不同之处,就是图像 URL 的格式,但是这应该如何产生这种不同呢?

记录成功的 getPicture (CAMERA):

onTakeSuccess imageURI=file:///mnt/sdcard/Android/data/com.XXX.YYY/cache/1404910577158.jpg
Code = 201
Response = "8999"
Sent = 195

记录不成功的 getPicture (LIBRARY):

onTakeSuccess imageURI= file:///mnt/sdcard/Android/data/com.XXX.YYY/cache/modified.jpg?1404910903858
upload error source file:///mnt/sdcard/Android/data/com.XXX.YYY/cache/modified.jpg?1404910903858
upload error target https://CORRECTURL/Umbraco/Api/ImageApi/PostUpload

此外,getPicture (LIBRARY) 情况会发出此警报:

Code = 1-400, image:file:///mnt/sdcard/Android/data/com.XXX.YYY/cache/modified.jpg?1404910903858

4

3 回答 3

1

我目前的解决方法是删除“?” 以及来自 imageUri 的所有内容。这样 URI 总是看起来像:

file:///mnt/sdcard/Android/data/com.XXX.YYY/cache/modified.jpg

在我的情况下,这可以正常工作并正确传输文件

于 2015-04-13T10:26:09.120 回答
1

使用cordova 6.0.0、cordova-plugin-camera 2.1.0、cordova-plugin-whitelist 1.2.1

我有类似的问题(尽管可能是由于不同的原因,因为它是较新的版本)。

我发现我需要在 config.xml 中有以下几行

<access origin="*" />
<access origin="content:///*" />

注意:只有

<access origin="*" />

还不够

于 2016-03-12T08:26:10.543 回答
0

是的!画廊中的一些文件是这种格式:文件名中的 modified.jpg?1438896451328。

为了防止,我在我的 upload.php 文件中放了一些代码:

        $temporary = explode(".", $_FILES["file"]["name"]);
        $file_extension = end($temporary);

   // cortamos caracteres extraños por si llega algun nombre de archivo raro
        $cadena = $file_extension; 
        $patron = "?"; 
   // localicamos en que posición se haya "?"
        $posicion = strpos ($cadena, $patron); 
   // eliminamos los caracteres desde ? hacia la derecha
        if ($posicion !== false){
        $file_extension = substr ($cadena, 0, $posicion); 
        }

然后您可以对过滤器文件类型,大小等进行操作并最终保存。

于 2015-08-06T22:05:32.427 回答