【发布时间】:2016-06-10 12:18:21
【问题描述】:
我有一个使用 cordova 文件传输插件创建的简单移动应用程序。下面是上传代码
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
在噩梦中无法弄清楚如何解决这个问题后,我决定在 iis 上托管一个 php 作为替代方案。 Cordova 文件传输插件似乎与herehere 的 php 服务器页面一起正常工作
【问题讨论】:
-
不太确定这是否是防火墙问题,但刚刚确认 Windows 防火墙在 localhost 运行的地方已关闭
-
尝试使用ip代替主机名(localhost)
-
@Gandhi 无效的问题与 127.0.0.1 相同。我认为问题在于请求标头无法在两个世界之间很好地遵守
-
好的。尝试在 options.headers = { 'Content-Type': 'image/jpg' } 中添加这个选项并检查一次
-
我在您的代码中注意到的另一件事是 options.headers = {Connection: "Close" };理想情况下应该是 options.headers = {'Connection':'Close' };
标签: asp.net cordova file-upload cordova-plugins jquery-file-upload