因此,要么没有人尝试这样做,要么我只是在上面找不到任何东西。您上传文件的旧方式是这样的:
public class FileStorage
{
public string FileName { get; set; }
public byte[] FileStore { get; set; }
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(HttpPostedFileBase file)
{
FileStorage fileAttachment = new FileStorage();
using (Stream inputStream = file.InputStream)
{
MemoryStream memoryStream = inputStream as MemoryStream;
//Check to see if stream returned is already a MemoryStream
if(memoryStream == null)
{
memoryStream = new MemoryStream();
inputStream.CopyTo(memoryStream);
}
fileAttachment.FileStore = memoryStream.ToArray();
fileAttachment.FileName = file.FileName;
}
if (ModelState.IsValid)
{
db.FileAttachment.Add(fileAttachment);
await db.SaveChangesAsync();
return RedirectToAction("Index"); …Run Code Online (Sandbox Code Playgroud)