gpt4 book ai didi

asp.net-mvc - 无法将 HttpFileCollectionBase 转换为 HttpFileCollection

转载 作者:行者123 更新时间:2023-12-03 23:30:42 33 4
gpt4 key购买 nike

我有一个片面的看法:

<% using (Html.BeginForm("add", "home", FormMethod.Post, 
new { enctype = "multipart/form-data" })){%>
<input name="IncomingFiles" type="file" />
<div class="editor-field"><%: Html.TextBox("TagsInput") %></div>
<p><input type="submit" value="Create" /></p><% } %>

在 Controller 中:

    [HttpPost]
public ActionResult add(HttpFileCollection IncomingFiles, string TagsInput)
{
return View();
}

它根本不会将我上传的文件与 HttpFileCollection 相匹配,它们会以 HttpFileCollectionBase 的形式出现。我怎样才能让 View 向我传递 HttpFileCollection?

我需要任何特定的 BeginForm 参数吗?

谢谢!

最佳答案

在你的行动方面做这样的事情。您不将文件作为参数传递:

[HttpPost]
public ActionResult add(string TagsInput) {
if (Request.Files.Count > 0) {
// for this example; processing just the first file
HttpPostedFileBase file = Request.Files[0];
if (file.ContentLength == 0) {
// throw an error here if content length is not > 0
// you'll probably want to do something with file.ContentType and file.FileName
byte[] fileContent = new byte[file.ContentLength];
file.InputStream.Read(fileContent, 0, file.ContentLength);
// fileContent now contains the byte[] of your attachment...
}
}
return View();
}

关于asp.net-mvc - 无法将 HttpFileCollectionBase 转换为 HttpFileCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3208661/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com