1

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.

I tried to resolve it using an @Html.ActionLink(), but no success. 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.

Here is my View - 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:

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.

tolik
  • 187
  • 1
  • 10
  • Possible duplicate of [How do you handle multiple submit buttons in ASP.NET MVC Framework?](https://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework) – CodeCaster Mar 28 '18 at 07:57
  • @CodeCaster as I mentioned, I don't want to check in the controller what is the value of the submit button.I was asking of possible other way to resolve. – tolik Mar 28 '18 at 07:59
  • Why do you have a submit button to do a download? - that should be a `` element. And your submit button will be to submit the form –  Mar 28 '18 at 08:00
  • And your `UploadImage()` method will not return `null` - that will just display a blank page - it should be returning the view if invalid, or redirecting if the file has been successfully saved –  Mar 28 '18 at 08:03

1 Answers1

2

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.

So your upload image Post Action method will be:

[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;
  }
Tanmay
  • 1,123
  • 1
  • 10
  • 20