1

at this moment I am uploading any file and saving it. What I want is to generate an error message if the user does not select any file and press the upload button, but at this moment the only thing it does is redirect to another view whether or not it has selected files. I would like to know if there is another better way to generate the upload of these files

this is my controller

public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public void Upload(HttpPostedFileBase file)
        {
            string file = (file.FileName).ToLower();

            try
            {
                file.SaveAs(Server.MapPath("~/Uploads/" + file));
            }
            catch (Exception e)
            {
                ViewBag.UploadError = "Upload file error";
            }           
        }
    }

this is the view:

@{
    ViewBag.Title = "Home";
}

@using (Html.BeginForm("Transformation", "Xml", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

    <div class="col-md-12 offset-md-5">
        <div class="custom-file col col-lg-2">
            <input type="file" name="file" class="custom-file-input" id="inputGroupFile01" aria-describedby="inputGroupFileAddon01">
            <label class="custom-file-label" for="inputGroupFile01">Choose file</label>
        </div>
        <div class="col-5">
            <button class="btn btn-success col-md-4 mt-2" type="submit">Upload file</button>
        </div>
    </div>

    //Message Error
    <div class="alert alert-danger" role="alert">
        <p class="text-danger">
            @ViewBag.UploadError
        </p>
    </div>
}

3 Answers 3

2

@Thomas Caycedo Martinez, I believe you can simply modify your controller method like below. If error, return to the same view with an error message. Your view remains unchanged.

    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase file)
    {
        if (file != null)
        {
            string fileName = (file.FileName).ToLower();
            try
            {
                file.SaveAs(Server.MapPath("~/Uploads/" + fileName));
            }
            catch (Exception e)
            {
                ViewBag.UploadError = "Upload file error";
                return View("Index");
            }
        }
        else {
            ViewBag.UploadError = "Upload file error";
            return View("Index");
        }

        return View();
    }
Sign up to request clarification or add additional context in comments.

Comments

1

write the action and controller correctly

@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))

and check file in action

[HttpPost]
        public ActionResult Upload(HttpPostedFileBase file)
        {
            if( file != null && file.Length > 0)
            {
                string file = (file.FileName).ToLower();
                try
                {
                    file.SaveAs(Server.MapPath("~/Uploads/" + file));
                }
                catch (Exception e)
                {
                    ViewBag.UploadError = "Upload file error";
                } 
               return View("Index");
            }
            else
            {
             //do something
                 return View("Index");
            }        
        }

3 Comments

Thank you for your answer @Farhad Zamani, in this case, I am writing this action and controller because I want to go to another view once the file is loaded
@ThomasCaycedoMartinez Excuse me, I read the question wrongly. I've changed it now
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
1

You can use JavaScript/JQuery to check for the same..

$(function(){

  $("#btnSubmit").on("click",function(){
    if($("#inputGroupFile01").val()==""){
        alert("Please select a file")
        return false;
    }
 })
})

where btnSubmit is the id for the submit button

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.