首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角2的Kendo UI上载组件-不能上传

角2的Kendo UI上载组件-不能上传
EN

Stack Overflow用户
提问于 2017-07-04 05:38:14
回答 2查看 2.8K关注 0票数 2

我是角2和web (.net核心)的kendo。我不能上传文件到网络api使用剑道上传。

下面是我的示例代码:Html:

代码语言:javascript
复制
    <kendo-upload [saveUrl]="uploadSaveUrl"
                          [removeUrl]="uploadRemoveUrl"
                          (upload)="uploadEventHandler($event)">
    </kendo-upload>

上传事件处理程序

代码语言:javascript
复制
     uploadEventHandler(e: UploadEvent) 
     {
         this.fs.uploadFile(e.files).subscribe(result => { console.log('result', result); });

     }

上传服务:

代码语言:javascript
复制
 uploadFile(file: any) 
 {

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

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

Web:

代码语言:javascript
复制
    [HttpPost("UploadFile")]
    public string UploadFile(IList<IFormFile> files)
    {

        return "";
    }

在这里,我无法获得api中的文件列表。有什么有用的代码吗?

EN

回答 2

Stack Overflow用户

发布于 2017-10-21 04:57:02

代码语言:javascript
复制
<kendo-upload #myUpload="kendoUpload" [autoUpload]="false" [saveUrl]="'/api/Attachment/PostFormData'"  (upload)="uploadEventHandler($event)"> </kendo-upload>

component.ts

代码语言:javascript
复制
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
        };
    }

网络Api控制器

代码语言:javascript
复制
[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);
            }
        }
票数 2
EN

Stack Overflow用户

发布于 2017-07-21 07:50:08

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

代码语言:javascript
复制
[HttpPost("UploadFile")]
public string UploadFile([FromForm]ICollection<IFormFile> files)
{
  return "";
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44897714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档