File upload not working

Thread is closed for posting
3 posts, 0 answers
  1. Kinrep
    Kinrep avatar
    2 posts
    Member since:
    Nov 2015

    Posted 11 Jun 2016 Link to this post


    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...";
                    options, true);
                function onFileUploadSuccess (result) {
                   // rst.innerHTML = "Upload successful";
                    console.log("Code = " + result.responseCode);
                    console.log("Response = " + result.response);
                    console.log("Sent = " + result.bytesSent);
                    console.log("Link to uploaded file:" + result.response);
                    var response = result.response;
                    var destination = "" + response.substr(response.lastIndexOf('=') + 1);
                    if( == 'uploadcheque') {
                        document.getElementById("hdnchequeimgpath").value = destination;
                    } else if( == 'uploaddoorlock') {
                        document.getElementById("hdndoorlockedimgpath").value = destination;
                    } else {
                        document.getElementById("hdnothersimgpath").value = destination;
                    rst.innerHTML = "File uploaded to: " +
                                                                  destination +
                                                                  "</br><button class=\"button\" onclick=\"'" + destination + "', '_blank', 'location=yes')\">Open Location</button>";
                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: " +;

    Below is the server code in ASP.NET

    public string SaveImage()
            HttpPostedFile file = HttpContext.Current.Request.Files[0];
            if (file == null)
                return "0";
            string targetFilePath = Server.MapPath(@"WS\ContentLibrary") + file.FileName;
        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 as given in example code it worked fine (i could see the uploaded image on 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?

    Ref. to post



  2. Kinrep
    Kinrep avatar
    2 posts
    Member since:
    Nov 2015

    Posted 11 Jun 2016 in reply to Kinrep Link to this post

    As alternative I decided to host php in my iis server as the solution is working fine with php. However this is not the answer.
  3. Anton Dobrev
    Anton Dobrev avatar
    609 posts

    Posted 15 Jun 2016 Link to this post


    Based on the debug information you provided in the SOF post it appears that the correct multipart form data and headers are incoming from the client app.

    I am assuming that the file is located in the Files property of the Request which is of type HttpFileCollection class (read more here -

    Perhaps you may consider accessing the files in the collection like:

    HttpPostedFile file = Request.Files["file"];


    / Get the HttpFileCollection.
    HttpFileCollection attachments = Request.Files;
    foreach (var fileKey in hfc.AllKeys)
      // enumerate the properties and determine which contains the files

    I believe the Request.Files[0]; is not accessing the correct member of the collection.

    I hope that this helps.

    Anton Dobrev

    Visit the Telerik Verified Plugins Marketplace and get the custom Cordova plugin you need, already tweaked to work seamlessly with AppBuilder.

Back to Top