问题
I'm trying to do a simple file upload in asp.net core 2 razor pages. I have the code below. Please realize that it is imcomplete. When i run in my VS2017, I check my FileUpload object, and it is unfortunately null. I would hope that it is something besides null and I could create a stream to get some data out of it. However, with the object being null, I would suspect that I do not have something tied together correctly. Any thoughts are appreciated. Thanks.
Code behind cs:
public class PicturesModel : PageModel
{
public PicturesModel()
{
}
[Required]
[Display(Name = "Picture")]
[BindProperty]
public IFormFile FileUpload { get; set; }
public async Task OnGetAsync()
{
}
public async Task<IActionResult> OnPostAsync()
{
//FileUpload is null
return RedirectToPage("/Account/Pictures");
}
}
front end cshtml file;
<form method="post" enctype="multipart/form-data">
<label asp-for="FileUpload"></label>
<input type="file" asp-for="FileUpload" id="file" name="file" />
<input type="submit" value="Upload" />
</form>
回答1:
Your property for the file input is named FileUpload
but you have overridden the name
attribute generated by the TagHelper and renamed it to file
which does not match the property name.
Change the view code to
<input type="file" asp-for="FileUpload" />
so that it generates the correct name
attribute (which is name="FileUpload"
). Note also the removal of id="file"
which means that you <label>
would not have set focus to the associated control when clicked).
回答2:
I spent ages trying all kinds of solutions this morning, and none seemed to work. But eventually, I came up with this... Enjoy.
/// <summary>
/// Upload a .pdf file for a particular [User] record
/// </summary>
[AllowAnonymous]
[HttpPost("uploadPDF/{UserId}")]
public async Task<IActionResult> uploadPDF(string UserId, IFormFile inputFile)
{
try
{
if (string.IsNullOrEmpty(UserId))
throw new Exception("uploadPDF service was called with a blank ID.");
Guid id;
if (!Guid.TryParse(RequestId, out id))
throw new Exception("uploadPDF service was called with a non-GUID ID.");
var UserRecord = dbContext.Users.FirstOrDefault(s => s.UserID == id);
if (UserRecord == null)
throw new Exception("User record not found.");
var UploadedFileSize = Request.ContentLength;
if (UploadedFileSize == 0)
throw new Exception("No binary data received.");
var values = Request.ReadFormAsync();
IFormFileCollection files = values.Result.Files;
if (files.Count == 0)
throw new Exception("No files were read in.");
IFormFile file = files.First();
using (Stream stream = file.OpenReadStream())
{
BinaryReader reader = new BinaryReader(stream);
byte[] bytes = reader.ReadBytes((int)UploadedFileSize);
Trace.WriteLine("Saving PDF file data to database..");
UserRecord.RawData = bytes;
UserRecord.UpdatedOn = DateTime.UtcNow;
dbContextWebMgt.SaveChanges();
}
return new OkResult();
}
catch (Exception ex)
{
logger.LogError(ex, "uploadPDF failed");
return new BadRequestResult();
}
}
回答3:
There is no files parameter in Post method.
Check tutorial here: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads
来源:https://stackoverflow.com/questions/47384127/file-upload-in-asp-net-core-2-razor-pages