File upload not working

3 posts, 0 answers
  1. Subbarao
    Subbarao avatar
    2 posts
    Member since:
    Nov 2015

    Posted 11 Jun Link to this post

    Hi,

    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 in ASP.NET

    [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?

    Ref. to post http://stackoverflow.com/questions/37748110/cannot-upload-files-from-android-to-asp-net-server?noredirect=1#comment62989790_37748110

     

     

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

    Posted 11 Jun in reply to Subbarao 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
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 15 Jun Link to this post

    Hi,

    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 - https://msdn.microsoft.com/en-us/library/system.web.httpfilecollection%28v=vs.110%29.aspx

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

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

    or

    / 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.

    Regards,
    Anton Dobrev
    Telerik
     

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

     
Back to Top