通过选择文件格式移动设备时如何在aspx页面中接收文件 [英] How receive file in aspx page when pass choose file form mobile device
问题描述
嗨
我使用cordova FileTransfer插件在Android和IOS Mobile设备上传和下载,我现在停止使用HttpPostedFile在aspx页面中接收
从移动应用程序我使用以下代码传输文件。
按钮点击事件
Hi
I am using cordova FileTransfer plugin to upload and download in Android and IOS Mobile device,i am now stuck with receive in aspx page using HttpPostedFile
From mobile app i am transfer file using below code.
in button click event
function uploadFile() {
navigator.camera.getPicture(
uploadPhoto,
function (message) {
$("#ErrorDiv").text("Failed to get a picture. Please select one.");
}, {
quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
});
function uploadPhoto(fileURI) {
var ft = new FileTransfer();
ft.upload(fileURI, encodeURI("http://" + localStorage.serverip + "/UService/ReceiveUpload.aspx"), UploadSuccess, UploadFail, options);
}
}
以上localStorage.serverip是服务器,在ReceiveUpload.aspx页面加载我写下面的代码来检索文件并保存在服务器机器文件夹中。
In above localStorage.serverip is server and in ReceiveUpload.aspx page load i write below code to retrive file and save in server machine folder.
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Request.Files.Count <= 0 Then
Exit Sub
End If
Dim MyFileCollection As HttpFileCollection
MyFileCollection = Request.Files
Dim postedFile As HttpPostedFile = Request.Files(0)
Dim inStream As System.IO.Stream = postedFile.InputStream
Dim fileData As Byte() = New Byte(postedFile.ContentLength - 1) {}
inStream.Read(fileData, 0, postedFile.ContentLength)
Dim writepath As String = Server.MapPath(".") & "\UserFiles"
postedFile.SaveAs(writepath)
End Sub
如果我从以下方式选择文件,它可以正常工作
图库 - >相机或
图库 - >屏幕截图或
图库 - > WhatsApp图像或
Gallert - >图片或来自Dropbox
这是截图,当我从上面选择http://prntscr.com/dlkeh7并且它正常工作。
但是如果使用下面的代码直接从照片,内部存储,SD卡转移文件中选择文件,这是截图http://prntscr.com/dlkebn或http://prntscr.com/dlke7k
Request.Files计数总是得到0.
fileURI有什么问题?在上面的屏幕截图中,我在警告信息中显示了fileURI。
我尝试过:
但是如果尝试上传以下代码可以从移动设备的任何位置转移
If i am choose files from following ways ,it work fine
Gallery --> Camera or
Gallery--> Screenshots or
Gallery-->WhatsApp Images or
Gallert--> Pictures or from Dropbox
this is screenshot when i choose from above ways http://prntscr.com/dlkeh7 and it working.
But if choose files directly from Photos,Internal Storage,SD card cant transfer file using below code ,this is screenshot http://prntscr.com/dlkebn or http://prntscr.com/dlke7k
Request.Files count always get 0.
What wrong with fileURI ? in above screenshot i showed fileURI in alert msg.
What I have tried:
But if try to upload with following code can transfer from any location from mobile devices
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
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 = {};
params.value1 = "test";
params.value2 = "param";
options.params = params;
var ft = new FileTransfer();
ft.upload(imageURI, encodeURI("http://posttestserver.com/post.php"), win, fail, options);
}
请尽快回复
问候
Aravind
Pls reply asap
Regards
Aravind
推荐答案
( #ErrorDiv)。text( 无法得到一张照片。请选择一张。);
},{
质量: 50 ,
destinationType: navigator .camera.DestinationType.FILE_URI,
sourceType: navigator .camera.PictureSourceType.PHOTOLIBRARY
});
function uploadPhoto(fileURI){
var ft = new FileTransfer();
ft.upload(fileURI, encodeURI ( http:// + localStorage.serverip + / UService / ReceiveUpload.aspx),UploadSuccess,UploadFail,options);
}
}
("#ErrorDiv").text("Failed to get a picture. Please select one."); }, { quality: 50, destinationType: navigator.camera.DestinationType.FILE_URI, sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY }); function uploadPhoto(fileURI) { var ft = new FileTransfer(); ft.upload(fileURI, encodeURI("http://" + localStorage.serverip + "/UService/ReceiveUpload.aspx"), UploadSuccess, UploadFail, options); } }
以上localStorage.serverip是服务器,在ReceiveUpload.aspx页面加载我写下面的代码来检索文件并保存在服务器机器文件夹中。
In above localStorage.serverip is server and in ReceiveUpload.aspx page load i write below code to retrive file and save in server machine folder.
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Request.Files.Count <= 0 Then
Exit Sub
End If
Dim MyFileCollection As HttpFileCollection
MyFileCollection = Request.Files
Dim postedFile As HttpPostedFile = Request.Files(0)
Dim inStream As System.IO.Stream = postedFile.InputStream
Dim fileData As Byte() = New Byte(postedFile.ContentLength - 1) {}
inStream.Read(fileData, 0, postedFile.ContentLength)
Dim writepath As String = Server.MapPath(".") & "\UserFiles"
postedFile.SaveAs(writepath)
End Sub
如果我从以下方式选择文件,它可以正常工作
图库 - >相机或
图库 - >屏幕截图或
图库 - > WhatsApp图像或
Gallert - >图片或来自Dropbox
这是截图,当我从上面选择http://prntscr.com/dlkeh7并且它正常工作。
但是如果使用下面的代码直接从照片,内部存储,SD卡转移文件中选择文件,这是截图http://prntscr.com/dlkebn或http://prntscr.com/dlke7k
Request.Files计数总是得到0.
fileURI有什么问题?在上面的屏幕截图中,我在警告信息中显示了fileURI。
我尝试过:
但是如果尝试上传以下代码可以从移动设备的任何位置转移
If i am choose files from following ways ,it work fine
Gallery --> Camera or
Gallery--> Screenshots or
Gallery-->WhatsApp Images or
Gallert--> Pictures or from Dropbox
this is screenshot when i choose from above ways http://prntscr.com/dlkeh7 and it working.
But if choose files directly from Photos,Internal Storage,SD card cant transfer file using below code ,this is screenshot http://prntscr.com/dlkebn or http://prntscr.com/dlke7k
Request.Files count always get 0.
What wrong with fileURI ? in above screenshot i showed fileURI in alert msg.
What I have tried:
But if try to upload with following code can transfer from any location from mobile devices
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
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 = {};
params.value1 = "test";
params.value2 = "param";
options.params = params;
var ft = new FileTransfer();
ft.upload(imageURI, encodeURI("http://posttestserver.com/post.php"), win, fail, options);
}
请尽快回复
问候
Aravind
Pls reply asap
Regards
Aravind
这篇关于通过选择文件格式移动设备时如何在aspx页面中接收文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!