无法使用 Cordova FileTransfer 插件将文件从 android 上传到 asp.net 服务器

Cannot upload files from android to asp.net server using Cordova FileTransfer plugin

我有一个使用 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 网络服务时无法正常工作。我看过其他各种帖子,但无法弄清楚出了什么问题。在客户端控制台中,它没有写入发送的字节,这意味着客户端没有问题,而服务器端似乎有问题。谁能指出问题出在哪里?

下面是调试输出

UPDATE-06112016-5:35PMIS 还是一头雾水也在http://www.telerik.com/forums/file-upload-not-working-93d711a97c9b

发帖

UPDATE-06112016-9-54PMIS

经过噩梦无法弄清楚如何解决这个问题后,我决定在 iis 上托管 php 作为替代方案。 Cordova 文件传输插件似乎与 php 服务器页面一起工作正常,因为 here

显然,IIS 7.5 在快速 CGI 模式下有一个关于分块多部分表单数据的错误:https://bugs.php.net/bug.php?id=60826

Cordova 的 FileTransfer 插件将分块模式默认为 true:

chunkedMode: Whether to upload the data in chunked streaming mode. Defaults to true. (Boolean)

在我的例子中,这正是问题所在,将 options.chunkedMode 设置为 true 立即修复了它。

请注意,您将无法再使用 onprogress 属性。

onprogress: Called with a ProgressEvent whenever a new chunk of data is transferred. (Function)