我正在使用一个 byte[]
来获取我想要上传到 ASP.NET 网站的文件的序列化版本。我在你的视图模型上使用 HttpPostedFileBase
来保存上传的文件。
public class MyViewModel
{
[Required]
public HttpPostedFileBase File { get; set; }
}
我的 HomeController
是
public class HomeController: Controller
{
public ActionResult Index()
{
var model = new MyViewModel();
return View(model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
if (!ModelState.IsValid)
return View(model);
byte[] uploadedFile = new byte[model.File.InputStream.Length];
model.File.InputStream.Read(uploadedFile, 0, uploadedFile.Length);
// Where do I write the file to?
return Content("Upload complete");
}
}
最后我认为,我已经达到了……(需要上下文才能确定具体翻译)
@model MyViewModel
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
@Html.LabelFor(x => x.File)
@Html.TextBoxFor(x => x.File, new { type = "file" })
@Html.ValidationMessageFor(x => x.File)
</div>
<button type="submit">Upload</button>
}
上传操作只会由我(管理员)执行,而且只有两个文件需要上传,都是应用程序安装程序(.exe文件,一个8MB和另一个18MB)。我的问题是,我该如何/在哪里存储已上传的文件,以便用户可以下载这些文件?
感谢您的时间。