sam
sam

Reputation: 287

mvc3 file upload

Whenever I try to upload a file to a server the current View is redirected to a different View from the controller. How can I upload a file and stay on the same View.

I have tried the following code:

public Action Result(HttpPostedFileBase file)
{
  return new EmptyResult();
}

Upvotes: 0

Views: 442

Answers (4)

Jitendra Pancholi
Jitendra Pancholi

Reputation: 7552

Suppose your view name is UploadView.cshtml and from there, you are uploading file.

UploadView.cshtml

@using (Html.BeginForm("UploadFile", "MyController", FormMethod.Post, new { enctype = "multipart/form-data", id = "frm", name = "frm" }))
{
<input id="FileAttachments" type="file" name="FileAttachments" />&nbsp;&nbsp; 
<input type="submit" value="upload" />
}

Your Controller would be MyController.cs

[HttpGet]
public ActionResult UploadView()
{
   Return View();
}

[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase FileAttachments)
{
    if (FileAttachments != null)
    {
       string fileName = System.Guid.NewGuid().ToString() + Path.GetFileName(FileAttachments.FileName);
       fileName = Path.Combine(Server.MapPath("~/Content/Files"), fileName);
       FileAttachments.SaveAs(fileName);
    }
    return View("UploadView");
}

Upvotes: 0

Viral Shah
Viral Shah

Reputation: 2246

Check my code for article submission in MVC architechture.

public ActionResult Submit(ArticleViewModel newSubmit, HttpPostedFileBase uploadFile)
{
    if (ModelState.IsValid)
    {
        //Upload File
        if (uploadFile != null)
        {
            string fileName = uploadFile.FileName;
            newSubmit.Article.image = fileName;
            uploadFile.SaveAs("~/Content/Uploads/Images");
            string savedFileName = Path.Combine(Server.MapPath("~/Content/Uploads/Images"), uploadFile.FileName);
        }
    // The HTML comes encoded so we decode it before insert into database
    newSubmit.Article.content = HttpUtility.HtmlDecode(newSubmit.Article.content);
    //Set article flags
    newSubmit.Article.flagged = true;
    newSubmit.Article.finished = false;
    newSubmit.Article.submitStoryFlag = true;
    //Insert article in the database                _repository.AddArticle(newSubmit);
    return View("Submitted");
}
    else
    {
       // Invalid – redisplay with errors
       return View(newSubmit);
    }
}

Upvotes: 0

Goran Žuri
Goran Žuri

Reputation: 1643

I would suggest using something like plupload for async upload. That way you can upload without redirect and even view image/document when upload is complete.

It allows multiple upload and fallback through different methods to successfully upload a file.

For implementation you just create another controller just for handling uploads.

Upvotes: 0

Alex
Alex

Reputation: 35399

Return View();

Should work as you'd expect, returning the View named Result.

If the current Action Method isn't the view you'd like to return you can use:

return RedirectToAction("actionmethodname");

Upvotes: 1

Related Questions