1

I am unable to upload file in folder. I am not able to find the mistake. The UploadFile View returns on same view after uploading file.

Model Class:

public class Upload
    {
        public int UploadId { get; set; }
        public string UploadTitle { get; set; }
        public string UploadURL { get; set; }

    }

Here is the Controller(FileUpload) Action:

public ActionResult UploadFile(HttpPostedFileBase file, Upload upload)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    string fil = System.IO.Path.GetFileName(file.FileName);
                    string path = System.IO.Path.Combine(Server.MapPath("/Content/Uploads/Files"), fil);
                    file.SaveAs(path);
                    upload.UploadURL = "/Content/Uploads/Files/" + file.FileName;
                }
                db.Uploads.Add(upload);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(upload);
        }

In my View:

@using (Html.BeginForm("UploadFile, "FileUpload", FormMethod.Post, new { enctype = "multipart/Form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            <div class="control-label col-md-2">
                <label for="file">Upload Image  for Slide:</label>
            </div>
            <div class="col-md-10">
                <input type="file" name="file" id="file" style="width:50%" />
            </div>

        </div>



        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}
7
  • Do you get an error? Did you try debugging the code? Does the Model.IsValid call return true? It's impossible to guess without some information Commented May 26, 2015 at 11:54
  • Do you have the "[HttpPost]" attribute before the function in Controller? Commented May 26, 2015 at 12:02
  • @Eru "[HttpPost]" attribute on the code. Commented May 26, 2015 at 12:11
  • @PanagiotisKanavos I didn't get any error. Commented May 26, 2015 at 12:16
  • Is your post action being hit ? Commented May 26, 2015 at 12:18

2 Answers 2

4

Hi I have tried your same code its works for me.

Controller

   [HttpPost]
        public ActionResult UploadFile(HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    string fil = System.IO.Path.GetFileName(file.FileName);
                    string path = System.IO.Path.Combine(Server.MapPath("/Content/Uploads/Files"), fil);
                    file.SaveAs(path);
                }
                return RedirectToAction("Index");
            }

            return View("UploadFile");
        }

View

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

<div class="form-horizontal">

    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-group">
        <div class="control-label col-md-2">
            <label for="file">Upload Image  for Slide:</label>
        </div>
        <div class="col-md-10">
            <input type="file" name="file" id="file" style="width:50%" />
        </div>

    </div>



    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}

I have found small mistake in you code in Html.BeginForm in action name " (double quotes is missing)

Sign up to request clarification or add additional context in comments.

2 Comments

it seems that you didn't use the model.
If you want to use the model you can use it and pass through the UploadFile action
-1

I forgot to mention the required field on UploadURL in above model class:

public class Upload
    {
        public int UploadId { get; set; }
        public string UploadTitle { get; set; }

        [Required]
        public string UploadURL { get; set; }

    }

Required Field validation on UploadURL field restricted the file upload here. I removed the Required field validation from the field.

1 Comment

Is this the answer or just additional info. just edit your post.

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.