[英]File upload ASP.NET MVC in multiple submits form
I have a small tool that downloads reports based on the specified options. 我有一个小工具,可根据指定的选项下载报告。 The download works well.
下载效果很好。 And now, I want to also upload a file to the folder and then further use it.
现在,我还想将文件上传到文件夹,然后再使用它。
The problem is that I already have one submit
button on the form that is used for the download and when I am adding another button for the upload, only download is triggered. 问题是我在用于下载的表单上已经有一个
submit
按钮,并且当我为上传添加另一个按钮时,仅触发下载。
I tried to resolve it using an @Html.ActionLink()
, but no success. 我试图使用
@Html.ActionLink()
解决它,但没有成功。 Is there any proper way to resolve the issue? 有没有解决此问题的适当方法? I know that there is a possibility to capture the
submit
value and then check in one main ActionResult
in the Controller
and redirect to the respective ActionResult
, but I don't want to do it, since there are too many POST
Actions in one controller. 我知道可以捕获
submit
值,然后在Controller
检入一个主要的ActionResult
并重定向到相应的ActionResult
,但是我不想这样做,因为一个控制器中的POST
操作太多。
Here is my View - download.cshtml: 这是我的视图 -download.cshtml:
@using (Html.BeginForm())
{
<fieldset>
<div class="title">Click to download report</div>
<div class="field">
<input id="downloadBtn" type="submit" class="button" value="Download" />
</div>
</fieldset>
<fieldset id="Option_ClientInfo">
<div class="title">
Image
</div>
<div class="field">
<input type="file" name="ImageUpload" accept="image/jpeg" />
<p>@Html.ActionLink("Upload", "UploadImage", new { controller = "Home", enctype = "multipart/form-data"}, new { @class = "button" })</p>
</div>
</fieldset>
}
And the controller - HomeController.cs: 和控制器 -HomeController.cs:
public partial class HomeController : Controller
{
// some functions
// ....
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UploadImage(HttpPostedFileBase imageFile)
{
string path = Path.Combine(this.GetImageFolder, Path.GetFileName(imageFile.FileName));
imageFile.SaveAs(path);
return null;
}
// additional POST functions for other forms
// ....
[HttpPost]
public ActionResult Download(Info downloadInfo)
{
// perform checks and calculations
return new reportDownloadPDF(downloadInfo);
}
}
Any suggestion in appreciated. 任何建议表示赞赏。
The solution is just separate upload and download functionalities using two forms so it wont conflict while submitting. 解决方案是使用两种形式的单独的上载和下载功能,因此提交时不会冲突。
@using (Html.BeginForm())
{
<fieldset>
<div class="title">Click to download report</div>
<div class="field">
<input id="downloadBtn" type="submit" class="button" value="Download" />
</div>
</fieldset>
<fieldset id="Option_ClientInfo">
<div class="title">
Image
</div>
</fieldset>
}
@using (Html.BeginForm("UploadImage", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<fieldset>
<div class="field">
<input type="file" name="ImageUpload" accept="image/jpeg" />
<p>
<input id="uploadBtn" type="submit" class="button" value="Upload" />
</p>
</div>
</fieldset>
}
There is another issue as well. 还有另一个问题。 Image control name and Post Action method parameter name should be same.
图像控件名称和Post Action方法参数名称应该相同。
So your upload image Post Action method will be: 因此,您的上传图片的Post Action方法将是:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UploadImage(HttpPostedFileBase imageUpload)
{
string path = Path.Combine(this.GetBasePath + "/img/tmp/", Path.GetFileName(imageFile.FileName));
imageFile.SaveAs(path);
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.