0

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

Community
  • 1
  • 1
Naga
  • 2,368
  • 3
  • 23
  • 33
  • not quite sure if this is firewall issue, but just confirmed that windows firewall is off where localhost is running – Naga Jun 10 '16 at 13:32
  • Try using the ip instead of hostname (localhost) – Gandhi Jun 10 '16 at 14:33
  • @Gandhi that did not work issue remained same with 127.0.0.1. I think the issue is with request header not able to comply well between 2 worlds – Naga Jun 10 '16 at 14:47
  • ok. Try adding this in options.headers = { 'Content-Type': 'image/jpg' } also in header option and check once – Gandhi Jun 10 '16 at 14:54
  • An other thing i noted in your code is options.headers = {Connection: "Close" }; Ideally it should be options.headers = {'Connection':'Close' }; – Gandhi Jun 10 '16 at 14:58
  • Adding Content-Type to header results error code 3, the issue remained same after changing Connection syntax. thanx anyways.. – Naga Jun 10 '16 at 16:01
  • what's the file name you are getting? – Gandhi Jun 10 '16 at 16:57
  • Also have you added whitelist plugin and required attributes in config.xml – Gandhi Jun 10 '16 at 17:01
  • There is already in config file I also add to manifest file still the issue remains same – Naga Jun 10 '16 at 17:36
  • how about whitelist plugin? Any error trace in console while debugging in device? – Gandhi Jun 10 '16 at 18:08
  • Havent yet added Whilelist plugin do I really need? Actually the same web service able to call other functions successfully. Will try with the plugin today. Thanks again.. – Naga Jun 11 '16 at 03:47
  • The above client code works fine when I change the url to filedropper,com hence whitelisting is not an issue is most likely the content-disposition in the header sent by client is not decoded properly at server end – Naga Jun 11 '16 at 04:32
  • Then you might focus on server side code as i m not well versed in asp.net – Gandhi Jun 11 '16 at 05:44

1 Answers1

2

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)

a.les
  • 36
  • 3