【问题标题】:Wrong file extension while uploading an image into server from android device从 android 设备将图像上传到服务器时文件扩展名错误
【发布时间】:2023-03-13 09:07:01
【问题描述】:

我正在尝试将图像从 android 设备库上传到服务器。但是选择的图片扩展名不是jpg/png/gif的形式。它以一些未知名称存储为“image%3A55766”。有人可以帮助我实现这一点。提前致谢。相同的代码在 iOS 设备上运行良好。我试过这个到现在。

<script>
function getPhoto(source) {
    navigator.camera.getPicture(onPhotoURISuccess, onFail, { 
		quality: 25,
		destinationType: Camera.DestinationType.FILE_URI,
		sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM
	});
}

function onPhotoURISuccess(imageURI) {
 	var options = new FileUploadOptions();
	options.fileKey="file";
	options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
	options.mimeType="image/jpeg";
	var params = new Object();
	options.params = params;
	var ft = new FileTransfer();		
	ft.upload(imageURI, encodeURI("http://server.net/project/php/uploads.php"), win, fail, options);   			
}

function win(r) {
	console.log("Code = " + r.responseCode);
	console.log("Response = " + r.response);
	console.log("Sent = " + r.bytesSent);
}		

function fail(error) {
	alert("An error has occurred: Code = " + error.code);    	
}     	
</script>

document.getElementById('gal1').innerHTML='<a href="javascript:void(0)" onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM)"><img src="../images/gallery.png"></a>';
			

以下是我在 Logcat 中获得的信息:

File locaton is: /storage/emulated/0/Pictures/Screenshots/Screenshot_2016-05-13-21-56-20.png
upload content://com.android.providers.media.documents/document/image%3A55766 to http://server.net/project/php/uploads.php
fileKey: file
fileName: image%3A55766
mimeType: image/jpeg
params: {}
trustEveryone: false
chunkedMode: true
headers: null
objectId: 1
httpMethod: POST
Content Length: 259650

【问题讨论】:

    标签: javascript android file cordova


    【解决方案1】:

    我坚信这个问题可能是由于您获取 imageURI 的方式造成的。这就是为什么这在 iOS 中可以正常工作但在 Android 中不能正常工作的原因,因为在这两个操作系统中文件系统的处理方式不同。我们设法在某个时候解决了类似的问题。问题在于文件名设置。请您看看这个link

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-15
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多