单轨文件上传
我想没有人了解这个吧?该文档糟糕而且已经过时了(我能找到的最好的资源是 2006 年的)。
我的形式:
<form action="DoCreate.rails" method="post">
${FormHelper.LabelFor("master.Name", "Name", {"class":"label"})}
${FormHelper.TextField("master.Name", {"class":"text-input full-width"})}
${FormHelper.LabelFor("masterFile", "File", {"class":"label"})}
<input type="file" id="masterFile" name="masterFile" />
<div class="edit-controls"><a href="/Master/Index.rails">Back</a> | <input type="submit" value="Create" /></div>
</form>
我的控制器操作:
public void DoCreate(Master master, HttpPostedFile masterFile)
{
try
{
Bus.Master.Create(master);
if (masterFile != null)
{
masterFile.SaveAs(@"C:\" + masterFile.FileName);
}
RedirectToAction("Index");
}
catch (ApplicationException e)
{
PropertyBag["error"] = e.Message + "<br />" + e.StackTrace;
Create();
RenderView("Create");
}
}
我遵循 本指南也无济于事,因为它没有告诉您在实际的 HTML 页面上要做什么。
I don't suppose anyone's clued up on this? The documentation is terrible and far outdated (the best resource I could find was dated 2006).
My form:
<form action="DoCreate.rails" method="post">
${FormHelper.LabelFor("master.Name", "Name", {"class":"label"})}
${FormHelper.TextField("master.Name", {"class":"text-input full-width"})}
${FormHelper.LabelFor("masterFile", "File", {"class":"label"})}
<input type="file" id="masterFile" name="masterFile" />
<div class="edit-controls"><a href="/Master/Index.rails">Back</a> | <input type="submit" value="Create" /></div>
</form>
My controller action:
public void DoCreate(Master master, HttpPostedFile masterFile)
{
try
{
Bus.Master.Create(master);
if (masterFile != null)
{
masterFile.SaveAs(@"C:\" + masterFile.FileName);
}
RedirectToAction("Index");
}
catch (ApplicationException e)
{
PropertyBag["error"] = e.Message + "<br />" + e.StackTrace;
Create();
RenderView("Create");
}
}
I followed this guide also with no avail as it doesn't tell you what to do on the actual HTML page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来问题出在表格的声明上。
上传文件时,您应该向表单元素添加另一个属性:
enctype="multipart/form-data"
Looks like the problem is with the declaration of the form.
When uploading files, you should use add another attribute to the form element:
enctype="multipart/form-data"