我有一個使用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
不能肯定這是否是防火牆的問題,而只是證實了Windows防火牆是關閉其中localhost運行 – Naga
嘗試使用IP而不是主機名(本地主機) – Gandhi
@Gandhi沒有工作問題未變與127.0.0.1。我認爲問題是請求標題不能很好地符合2個世界 – Naga