Ionic [2] Code 1 when uploading with file transfer

I’ve been using the file transfer plugin without problems now. But since a few days I’m getting an error when uploading a image. I have no idea what changed.

I’m getting code 1 which equals to FileTransferError.FILE_NOT_FOUND_ERR. But what does it mean? Does it have trouble finding the image on my phone? Or finding the image on the server? Anyone have any idea?

I’m getting a FileTransferError which returns a 500 status and an error has occurred message but that all it returns. Also it goes to 100% and then it gives the error.

My server-side code.

/// <summary>
/// Saves a collection item and adds it to the collection
/// </summary>
/// <returns></returns>
[HttpPost, Route("collections/save")]
public async Task<IHttpActionResult> createItem()
{
    if (!Request.Content.IsMimeMultipartContent())
        return InternalServerError(new Exception("UnsupportedMediaType"));

    var provider = new CustomMultipartFormDataStreamProvider(ApiSettings.CollectionBasePath);

    try
    {
        // Load the stream into buffer
        Request.Content.LoadIntoBufferAsync().Wait();

        // Read all contents of multipart message into CustomMultipartFormDataStreamProvider.
        await Request.Content.ReadAsMultipartAsync(provider);
    }
    catch (Exception ex)
    {
        return InternalServerError(new Exception($"Failed to read: {ex.Message}"));
    }

    return Ok();          
}

/// <summary>
/// Used to override an action within the MultipartFormDataStreamProvider
/// This is needed to format the file name of a posted file
/// </summary>
internal class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
{
    public CustomMultipartFormDataStreamProvider(string path) : base(path) { }

    /// <summary>
    /// Takes the bodypart off of the filename string
    /// </summary>
    /// <param name="headers">the headers to mutate</param>
    /// <returns>mutated filename</returns>
    public override string GetLocalFileName(HttpContentHeaders headers)
    {
        return headers.ContentDisposition.FileName.Replace(""", string.Empty);
    }
}

My app code

upload (baseUrl: string, string image, onSuccess: any, onFailed: any, onProgress: any) : void {
    var ft = new FileTransfer();  
    var options = new FileUploadOptions();

    options.fileKey = "file";
    options.fileName = filename;
    options.mimeType = "image/jpeg"
    options.chunkedMode = false;
    options.headers = { 
        'Content-Type' : undefined
    }

    ft.onprogress =  (e: ProgressEvent) => onProgress(e);  
    ft.upload(image, baseUrl + "collections/save", onSuccess, onFailed, options);            
}

failed = (err: any) : void => {
    var code = err.code;
    console.log(err);
    alert("Failed to upload image. Code: " + code);
}

onProgress =  (progressEvent: ProgressEvent) : void => {
    if (progressEvent.lengthComputable) {
        var progress = Math.round((progressEvent.loaded / progressEvent.total) * 100);
        //this.progress = progress;
        this.setProgress(progress);       
    }
}

success = (result: any) : void => {            
    this.current++;         
    if(this.current <= this.total) {          
        // this.progress = 0;            
        this.setCurrent(this.current);   
        this.setProgress(0);            
        setTimeout(() : void => {     
            // give the animation time to reset           
            this.upload(this.collection.items[this.current - 1]);
        },1000);
    } else {   
        this.finished = true;
    }
}

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

OK the problem was serverside. Thanks to this answer.

The problem was that I was uploading large files but the server did not accept them. The error I was getting is still very unhelpful though.

By setting the following in the web.config of my API

<system.web>
    <httpRuntime maxRequestLength="30000000" />
</system.web>

<system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="30000000" />
      </requestFiltering>
    </security>
</system.webServer>

I allowed larger files to be uploaded.

PS: Thanks for the downvote and not explaining why you downvoted it. Good job.