Ionic [2] 使用文件传输上传时的代码 1

Ionic [2] Code 1 when uploading with file transfer

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

我得到的代码 1 等于 FileTransferError.FILE_NOT_FOUND_ERR。但是这是什么意思?在我的 phone 上查找图片有困难吗?或者在服务器上查找图像?有人知道吗?

我收到 FileTransferError 一条 returns 500 状态和 an error has occurred 消息,但仅此而已 returns。它也达到 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;
    }
}

好的,问题出在服务器端。感谢 this 的回答。

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

通过在我的 API

的 web.config 中设置以下内容
<system.web>
    <httpRuntime maxRequestLength="30000000" />
</system.web>

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

我允许上传更大的文件。

PS:感谢您投反对票,但没有解释您投反对票的原因。干得好。