3

I am having an issue on the controller side using ASP.Net MVC 3 trying to upload an image to amazon s3. Here is what I have and the error.

Here is my HTML form.

@using (Html.BeginForm())
{
    <div class="in forms">

        <input type="file" id="file" name="file" class="box" /></p>

        <p><input type="submit" value="Upload" id="btnSubmit" class="com_btn" /></p>

}

And here is the code in my controller

[HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client("*redacted*","*redacted*");
        if (file.ContentLength > 0)
        {
            var request = new PutObjectRequest();
            request.WithBucketName("*redacted*");
            request.WithKey(file.FileName);
            request.FilePath = Path.GetFullPath(file.FileName);
            request.ContentType = file.ContentType;
            request.StorageClass = S3StorageClass.ReducedRedundancy;
            request.CannedACL = S3CannedACL.PublicRead;
            client.PutObject(request);
            return Redirect("UploadSuccess");
        }
        return RedirectToAction("Index");
    }

The error I am getting is.

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 28: if (file.ContentLength > 0)

3 Answers 3

4

Does this help?

using (@Html.BeginForm(new { enctype = "multipart/form-data" }))

You've Been Haacked - Uploading Files

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

3 Comments

This didn't change anything unfortunetly
Change it to this: @using (Html.BeginForm("Index", "YourController", FormMethod.Post, new { enctype = "multipart/form-data" }))
@using (Html.BeginForm("Index", "YourController", FormMethod.Post, new { enctype = "multipart/form-data" })) <-- This Fixed It
1

Use break points and debug the code. See whether your objects are null or not. Accessing a property / method of a null object will give you this error usually.

1 Comment

I have done this HttpPostedFileBase file is coming up as null for some reason even though I am passing the input type of file from the chtml page.
1
@using (Html.BeginForm("Index", "YourController", FormMethod.Post, new { enctype = "multipart/form-data" })) 

and change

if (file.ContentLength > 0)

to

if (file == null || file.ContentLength <= 0)
{
    // Add some client side error message.

    // Return the view
    return View();
}

// Upload...
var request = new PutObjectRequest();
...

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.