Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 71531

Code 1 when uploading with file transfer

$
0
0

@dtaalbers wrote:

Hi,

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

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

Im getting a FileTransferError wich returns a 500 status and a an error has occured message but that all it returns. Also it goes to 100% and then it gives the error.

My serverside 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;
    }
}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 71531

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>