そのため、まだ誰もこれを試みていないか、何も見つけられていません。ファイルをアップロードする古い方法は次のとおりです。
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");
}
return RedirectToAction("Index");
}
.Net Core が使用していることは知っていますIFormFile
が、それを保存するために見つけたすべてのリソースは、Web サーバーの wwwroot フォルダーに保存することについて話しています。ファイルをコントローラーに正常に渡していますがbyte[]
、DBFileStream
テーブルに保存するためにファイルを変換する方法がわかりません。
これが私の現在のコードです:
[HttpPost]
public async Task<IActionResult> Upload(IFormFile uploadFile)
{
if (ModelState.IsValid)
{
var parsedContent = ContentDispositionHeaderValue.Parse(uploadFile.ContentDisposition);
var fileName = Path.Combine(_hostingEnvironment.WebRootPath, "Uploads", parsedContent.FileName.Trim('"'));
await uploadFile.SaveAsAsync(fileName);
}
return View("Index");
}
DBで動作するように変換するにはどうすればよいですかFileStream