【问题标题】:Kendo UI upload component for angular 2 - Cannot uploadAngular 2的Kendo UI上传组件-无法上传
【发布时间】:2017-07-04 05:38:14
【问题描述】:

我是 angular 2 和 web api(.net core) 的 kendo ui。我无法使用剑道上传将文件上传到 web api。

这是我的示例代码: HTML:

    <kendo-upload [saveUrl]="uploadSaveUrl"
                          [removeUrl]="uploadRemoveUrl"
                          (upload)="uploadEventHandler($event)">
    </kendo-upload>

上传事件处理程序

     uploadEventHandler(e: UploadEvent) 
     {
         this.fs.uploadFile(e.files).subscribe(result => { console.log('result', result); });

     }

上传服务:

 uploadFile(file: any) 
 {

    const baseUrl = this.basePath + '/api/Common/UploadFile';

    return this.dah.post(baseUrl, file);
}

网络接口:

    [HttpPost("UploadFile")]
    public string UploadFile(IList<IFormFile> files)
    {

        return "";
    }

在这里,我无法在 api 中获取文件列表。有工作代码吗??

【问题讨论】:

    标签: angular asp.net-core-webapi kendo-ui-angular2 kendo-upload


    【解决方案1】:
    <kendo-upload #myUpload="kendoUpload" [autoUpload]="false" [saveUrl]="'/api/Attachment/PostFormData'"  (upload)="uploadEventHandler($event)"> </kendo-upload>
    

    component.ts

    ploadEventHandler(e: UploadEvent) {
            console.log(e.files[0].uid);
            // you can send extra data here
            e.data = {
                attachmentType: this.typList.filter(x=>x.Uid == e.files[0].uid)[0].type
            };
        }
    

    Web API 控制器

    [HttpPost]
            public async Task<HttpResponseMessage> PostFormData()
            {
                // Check if the request contains multipart/form-data.
                if (!Request.Content.IsMimeMultipartContent())
                {
                    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                }
    
                string _property = System.Web.HttpContext.Current.Request.Form["attachmentType"];
    
                string root = HttpContext.Current.Server.MapPath("~/App_Data/uploads");
                var provider = new MultipartFormDataStreamProvider(root);
    
                try
                {
                    // Read the form data.
                    await Request.Content.ReadAsMultipartAsync(provider);
                    int i = 0;
                    // This illustrates how to get the file names.
                    foreach (MultipartFileData fileData in provider.FileData)
                    {
                        if (string.IsNullOrEmpty(fileData.Headers.ContentDisposition.FileName))
                        {
                            return Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted");
                        }
                        string fileName = fileData.Headers.ContentDisposition.FileName;
                        if (fileName.StartsWith("\"") && fileName.EndsWith("\""))
                        {
                            fileName = fileName.Trim('"');
                        }
                        if (fileName.Contains(@"/") || fileName.Contains(@"\"))
                        {
                            fileName = Path.GetFileName(fileName);
                        }
                        var ext = Path.GetExtension(fileName);
                        var uniqFileName = $@"{Guid.NewGuid() + "." + ext }";
                        File.Move(fileData.LocalFileName, Path.Combine(root, uniqFileName));
                        i++;
                    }
                    return Request.CreateResponse(HttpStatusCode.OK);
                }
                catch (System.Exception e)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
                }
            }
    

    【讨论】:

    • 我们如何发送其他参数,比如应用 ID。因此,当我们为其创建记录时,我们可以将此文件与该 applicationId 相关联?
    • 看到我正在发送其他参数 e.data = { attachmentType: this.typList.filter(x=>x.Uid == e.files[0].uid)[0].type } ; // attachmentType is formdata Receiving :- string _property = System.Web.HttpContext.Current.Request.Form["attachmentType"]; // c#
    【解决方案2】:

    你没有说 'this.dah' 是什么,但是当使用 kendo-upload 'uploadFiles()' 方法时,你可以通过 web-api 服务中的 [FromForm] 属性访问你的文件:

    [HttpPost("UploadFile")]
    public string UploadFile([FromForm]ICollection<IFormFile> files)
    {
      return "";
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      相关资源
      最近更新 更多