Search code examples
asp.netcordovafile-uploadcordova-pluginsjquery-file-upload

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


I have a simple mobile app created using cordova file transfer plugin. Below is the upload code

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);
            }
}

Below is the server code

[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";

}

When the call gets invoked it is getting inside SaveImage webmethod but HttpContext.Current.Request.Files.Count is 0. The same call when I point to filedropper.com as given in example code it worked fine (i could see the uploaded image on filedrop.com) but not working when pointing to my windows web service. I have seen various other posts but could not just figure out whats going wrong. In the client console it writes no of bytes sent which means there is no issue from client side where as server side there seems to be an issue. Can anyone suggest where the issue is?

Below is the debug output enter image description here

UPDATE-06112016-5:35PMIS Still clueless also posted in http://www.telerik.com/forums/file-upload-not-working-93d711a97c9b

UPDATE-06112016-9-54PMIS

After a nightmare not been able to figure out how to fix the issue I decided to go with hosting a php on iis as alternative. Cordova File Transfer plugin seems to work fine with php server page as here


Solution

  • Apparently, IIS 7.5 in fast CGI mode has a bug regarding chunked multipart form data: https://bugs.php.net/bug.php?id=60826

    The FileTransfer plugin for Cordova defaults the chunked mode to true:

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

    In my case this was exactly the problem and setting options.chunkedMode to true fixed it right away.

    Note that you won't be able to use the onprogress property anymore.

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