gpt4 book ai didi

javascript - Phonegap camera.getPicture 上传问题

转载 作者:行者123 更新时间:2023-11-29 17:54:19 25 4
gpt4 key购买 nike

我的移动应用程序有两个目标。

a) 用手机相机拍照并上传到服务器
b) 从手机相册中选择图片并上传到服务器

我一直在使用 Phonegap,点“a”效果很好。还有“b”。

问题:似乎每秒上传都失败。不管我是从“a”点还是“b”点开始。或随机组合。

结果:
1.成功
2.失败
3. 成功
4.失败
4.等

这是我的点“a”的代码:

function getImageCamera() {
navigator.camera.getPicture(uploadPhotoCamera, function(message) {
},{
targetWidth: 1200,
targetHeight: 900,
quality: 75,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.CAMERA
}
);
}

对于点“b”:

function getImageGallery() {
navigator.camera.getPicture(uploadPhotoGallery, function(message) {
},{
targetWidth: 1200,
targetHeight: 900,
quality: 75,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.SAVEDPHOTOALBUM
}
);
}

uploadPhotoCamera 和 uploadPhotoGallery 函数相同。

这是 Eclipse 日志:

12-14 22:44:15.303: W/FileTransfer(32613): Error getting HTTP status code from connection.
12-14 22:44:15.303: W/FileTransfer(32613): java.io.EOFException
12-14 22:44:15.303: W/FileTransfer(32613): at libcore.io.Streams.readAsciiLine(Streams.java:203)
12-14 22:44:15.303: W/FileTransfer(32613): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:573)
12-14 22:44:15.303: W/FileTransfer(32613): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:821)
12-14 22:44:15.303: W/FileTransfer(32613): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
12-14 22:44:15.303: W/FileTransfer(32613): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:495)
12-14 22:44:15.303: W/FileTransfer(32613): at org.apache.cordova.FileTransfer$1.run(FileTransfer.java:484)
12-14 22:44:15.303: W/FileTransfer(32613): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-14 22:44:15.303: W/FileTransfer(32613): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-14 22:44:15.303: W/FileTransfer(32613): at java.lang.Thread.run(Thread.java:856)
12-14 22:44:15.311: E/FileTransfer(32613): {"target":"http:\/\/some_IP\/api_upload.php","source":"file:\/\/\/storage\/emulated\/0\/Android\/data\/com.tisamobile\/cache\/resize.jpg?1387057455160","http_status":0,"code":3}
12-14 22:44:15.311: E/FileTransfer(32613): java.io.EOFException
12-14 22:44:15.311: E/FileTransfer(32613): at libcore.io.Streams.readAsciiLine(Streams.java:203)
12-14 22:44:15.311: E/FileTransfer(32613): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:573)
12-14 22:44:15.311: E/FileTransfer(32613): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:821)
12-14 22:44:15.311: E/FileTransfer(32613): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
12-14 22:44:15.311: E/FileTransfer(32613): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:495)
12-14 22:44:15.311: E/FileTransfer(32613): at org.apache.cordova.FileTransfer$1.run(FileTransfer.java:484)
12-14 22:44:15.311: E/FileTransfer(32613): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-14 22:44:15.311: E/FileTransfer(32613): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-14 22:44:15.311: E/FileTransfer(32613): at java.lang.Thread.run(Thread.java:856)
12-14 22:44:15.311: E/FileTransfer(32613): Failed after uploading 103425 of 103425 bytes.

有什么问题吗?任何建议表示赞赏。

最佳答案

这不是 getPicture 函数的问题。

我找到了非常简单的解决方案。

在我看来,cordova 插件的 FileTransfer() 不会将有关文件传输结束的数据发送到服务器。

因此,服务器无法识别每个文件的边界,并出现问题。

var url = "http://www.upload-server-domain";
var imgFileURI = '/filepath/test.jpg';
var ft = new FileTransfer();

ft.upload('', encodeURI(url), null, null, null); // SOLUTION : fake upload
ft.upload(imgFileURI, encodeURI(url), fnSuccessCallback, fnErrorCallback, options); // real upload

我认为虚假上传会让服务器知道文件传输结束。

因为,Content-Length 为 0。

关于javascript - Phonegap camera.getPicture 上传问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20588326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com