Ejemplo n.º 1
0
        public async Task <IActionResult> UpdataFile([Bind("FormFile")] UpFileInput input)
        {
            if (!ModelState.IsValid)
            {
                return(Content("<script>alert('Please correct the form.')</script>", "text/html"));
            }

            var formFileContent =
                await FilesOptionHelper.ProcessFormFile <UpFileInput>(
                    input.FormFile, ModelState, _permittedExtensions,
                    _fileSizeLimit);

            if (!ModelState.IsValid)
            {
                return(Content("<script>alert('Please correct the form.')</script>"));
            }

            // For the file name of the uploaded file stored
            // server-side, use Path.GetRandomFileName to generate a safe
            // random file name.
            var trustedFileNameForFileStorage = Path.GetRandomFileName();
            var filePath = Path.Combine(
                _targetFilePath, trustedFileNameForFileStorage);

            // **WARNING!**
            // In the following example, the file is saved without
            // scanning the file's contents. In most production
            // scenarios, an anti-virus/anti-malware scanner API
            // is used on the file before making the file available
            // for download or for use by other systems.
            // For more information, see the topic that accompanies
            // this sample.

            using (var fileStream = System.IO.File.Create(filePath))
            {
                await fileStream.WriteAsync(formFileContent);

                // To work directly with a FormFile, use the following
                // instead:
                //await FileUpload.FormFile.CopyToAsync(fileStream);
            }
            return(RedirectToAction(nameof(Index)));
        }