This is a migrated thread and some comments may be shown as answers.

File upload not working

2 Answers 216 Views
Apache Cordova
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Kinrep
Top achievements
Rank 1
Kinrep asked on 11 Jun 2016, 11:59 AM

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 Answers, 1 is accepted

Sort by
0
Kinrep
Top achievements
Rank 1
answered on 11 Jun 2016, 05:50 PM
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.
0
Anton Dobrev
Telerik team
answered on 15 Jun 2016, 09:05 AM
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.

 
Tags
Apache Cordova
Asked by
Kinrep
Top achievements
Rank 1
Answers by
Kinrep
Top achievements
Rank 1
Anton Dobrev
Telerik team
Share this question
or