使用Cordova FileTransfer插件无法将文件从android上传到asp.net服务器
我有一个使用cordova文件传输插件创建的简单移动应用程序。下面是上传代码使用Cordova FileTransfer插件无法将文件从android上传到asp.net服务器
function uploadPhoto(fileURI) {
var options = new FileUploadOptions();
options.fileKey = fileURI.substr(fileURI.lastIndexOf('/') + 1);
options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
if (cordova.platformId == "android") {
options.fileName += ".jpg"
}
options.mimeType = "image/jpeg";
//options.contentType = 'multipart/form-data';
options.params = {}; // if we need to send parameters to the server request
options.headers = {
Connection: "Close"
};
//options.httpMethod = 'POST';
//options.chunkedMode = false;
var ft = new FileTransfer();
rst.innerHTML = "Upload in progress...";
ft.upload(
fileURI,
encodeURI("http://localhost:55013/virtualroomservice.asmx/SaveImage"),
onFileUploadSuccess,
onFileTransferFail,
options, true);
function onFileUploadSuccess (result) {
// rst.innerHTML = "Upload successful";
console.log("FileTransfer.upload");
console.log("Code = " + result.responseCode);
console.log("Response = " + result.response);
console.log("Sent = " + result.bytesSent);
console.log("Link to uploaded file: https://www.kinrep.com/foster/ws/contentlibrary" + result.response);
var response = result.response;
var destination = "https://www.kinrep.com/foster/WS/ContentLibrary" + response.substr(response.lastIndexOf('=') + 1);
if(this.id == 'uploadcheque') {
document.getElementById("hdnchequeimgpath").value = destination;
} else if(this.id == 'uploaddoorlock') {
document.getElementById("hdndoorlockedimgpath").value = destination;
} else {
document.getElementById("hdnothersimgpath").value = destination;
}
rst.innerHTML = "File uploaded to: " +
destination +
"</br><button class=\"button\" onclick=\"window.open('" + destination + "', '_blank', 'location=yes')\">Open Location</button>";
//document.getElementById("downloadedImage").style.display="none";
}
function onFileTransferFail (error) {
rst.innerHTML = "File Transfer failed: " + error.code;
console.log("FileTransfer Error:");
console.log("Code: " + error.code);
console.log("Source: " + error.source);
console.log("Target: " + error.target);
}
}
下面是服务器代码
[WebMethod]
[ScriptMethod]
public string SaveImage()
{
try
{
HttpPostedFile file = HttpContext.Current.Request.Files[0];
if (file == null)
return "0";
string targetFilePath = Server.MapPath(@"WS\ContentLibrary") + file.FileName;
file.SaveAs(targetFilePath);
}
catch (Exception ex)
{
string s = ex.Message;
return s;
}
return "1";
}
当呼叫被调用它里面SaveImage的WebMethod但HttpContext.Current.Request.Files.Count越来越为0的相同当我指向filedropper.com时,如果在示例代码中给出它的工作正常(我可以在filedrop.com上看到上传的图像),但在指向我的Windows Web服务时无法正常工作。我看到过其他各种帖子,但不能明确发生什么问题。在客户端控制台中,它写入没有发送的字节,这意味着从客户端没有问题,作为服务器端似乎存在问题。任何人都可以提出问题在哪里?
UPDATE-06112016-5:35PMIS 仍然毫无头绪也张贴在http://www.telerik.com/forums/file-upload-not-working-93d711a97c9b
UPDATE-06112016-9-54PMIS
在一场恶梦无法弄清楚如何解决这个问题后,我决定在i上托管一个php是可选的。科尔多瓦文件传输插件似乎与PHP服务器页面here
显然,做工精细,快速CGI模式IIS 7.5有关于分块多形式的数据中的错误:https://bugs.php.net/bug.php?id=60826
的文件传输插件科尔多瓦默认的分块模式true:
chunkedMode:是否以分块流模式上传数据。默认为true。 (布尔)
在我的情况下,这正是问题和设置options.chunkedMode真正解决它马上。
请注意,您将无法再使用onprogress属性。
进展中:每次传输新的数据块时都调用ProgressEvent。 (功能)
还没有尝试使用此设置,曾经有一段时间,我离开了这一边。感谢通过' – Naga
不能肯定这是否是防火墙的问题,而只是证实了Windows防火墙是关闭其中localhost运行 – Naga
尝试使用IP而不是主机名(本地主机) – Gandhi
@Gandhi没有工作问题未变与127.0.0.1。我认为问题是请求标题不能很好地符合2个世界 – Naga