2

This is my Controller:

public ActionResult Create()

    {
        ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName");
        return View();
    } 

    //
    // POST: /ManagePhotos/Create

    [HttpPost]
    public ActionResult Create(Photo photo, HttpPostedFile file)
    {
        if (ModelState.IsValid)
        {
            if (file != null && file.ContentLength > 0)
            {
                // save as original size of image
                var newfileName = Guid.NewGuid().ToString() + "_"
                                    + Path.GetFileName(file.FileName);
                var bigImagePath = Path.Combine(Server.MapPath("~/Content/PublicPhotos/BigImages"), newfileName);
                file.SaveAs(bigImagePath);

                // save as thumbnail image
                var photoUploaded = new WebImage(bigImagePath);
                photoUploaded.Resize(width: 200, height: 150, preserveAspectRatio: true, preventEnlarge: true);

                var thumbImagePath = Path.Combine(Server.MapPath("~/Content/PublicPhotos/ThumbImages"), newfileName);

                photoUploaded.Save(thumbImagePath);

            }

            db.Photos.Add(photo);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }

This is my View:

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

@Html.ValidationSummary(true)
<fieldset>
    <legend>Photo</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.CategoryID, "Category")
    </div>
    <div class="editor-field">
        @Html.DropDownList("CategoryID", String.Empty)
        @Html.ValidationMessageFor(model => model.CategoryID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>

    <div class="editor-label">
        @Html.Label("File name: ")
    </div>
    <div class="editor-field">
        <input type="file" name="file"/>
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

}

My Question: When I press "Create" button , the webpage show me below:

The connection was reset.

The connection to the server was reset while the page was loading.

The site could be temporarily unavailable or too busy. Try again in a few moments. If you are unable to load any pages, check your computer's network connection. If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web.

I try to debug in Razor View , and lookup on this website and I did exactly the website showed me but I cannot figure at out. Please help me.

4
  • What is the size of the file you are trying to upload ? Commented May 15, 2012 at 19:14
  • 2
    If your file exceed 4MB, check this: stackoverflow.com/questions/8822860/… Commented May 15, 2012 at 20:06
  • 1
    yes, you are right, I did it. Thank you very much Commented May 16, 2012 at 17:45
  • Please consider marking this question as answered Commented May 27, 2012 at 2:32

1 Answer 1

1

Your action is expecting HttpPostedFile but you should be using HttpPostedFileBase

Also as stated in the comments you need to verify the size isn't too large you can do that in Javascript

function onSelectImage(e) {
    if (e.files[0].size > 256000) {
        alert('The file size is too large for upload');
        e.preventDefault();
        return false;
    }
    ...
    return true;
}

This ensures an image is only 256K or less as an example

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

1 Comment

@Brandon Let me know if that helps you out any

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.