离线[2]上传文件传输时的代码1

时间:2016-04-12 20:21:34

标签: asp.net angular ionic2

我一直在使用文件传输插件而没有问题。但是几天后我在上传图片时遇到错误。我不知道改变了什么。

我得到的代码1等于FileTransferError.FILE_NOT_FOUND_ERR。但是这是什么意思?在手机上找到图像有困难吗?或者在服务器上找到图像?有人有任何想法吗?

我收到一个FileTransferError,它会返回500状态和an error has occurred消息,但它会全部返回。它也会达到100%,然后就会出错。

我的服务器端代码。

/// <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);
    }
}

我的应用代码

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 个答案:

答案 0 :(得分:0)

好的问题是服务器端。感谢this回答。

问题是我上传了大文件,但服务器不接受它们。我得到的错误仍然是非常无益的。

通过在我的API的web.config中设置以下内容

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

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

我允许上传更大的文件。

PS:感谢你的downvote并没有解释为什么你贬低它。干得好。