简体   繁体   中英

Kendo UI upload component for angular 2 - Cannot upload

I am kendo ui for angular 2 and web api(.net core). I cannot upload files to web api using kendo upload.

Here is my sample code: Html:

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

Upload event handler

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

     }

Upload service:

 uploadFile(file: any) 
 {

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

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

Web api:

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

        return "";
    }

Here, I cannot get files list in api. Is there any working code??

<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 Controller

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

You didn't say what 'this.dah' is, but when using the kendo-upload 'uploadFiles()' method you can access your files via [FromForm] Attribute in your web-api service:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM