Exemplo n.º 1
0
        public async Task <IActionResult> FileUpload()
        {
            try
            {
                string file_name   = Path.GetRandomFileName();
                string root_path   = _hostingEnvironment.ContentRootPath;
                var    upload_path = this.GetFilesUploadPath(true);

                var fileUploadForm = await FileUploadHelper.ParseRequestForm(this, async (section, formFileInfo) =>
                {
                    // TODO: This function will be called every time parser got a file
                    // but this controller only supports one file per request.
                    // Therefore the last file will be the one to be saved if the client
                    // passed up more than one file.

                    file_name           += Path.GetExtension(formFileInfo.FileName);
                    var upload_file_path = Path.Combine(upload_path, file_name);

                    using (var fileStream = System.IO.File.Create(upload_file_path))
                    {
                        await section.Body.CopyToAsync(fileStream);
                    }
                }, new FileUploadForm());

                return(Content(file_name));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }