0

I am trying to build an ecommerce application . My task is to store an image which the user uploads

from his machine into the oracle database . I have created a blob datatype for the column which

stores the image . Now

View Code

@using (Html.BeginForm())
{
  <div class="editor-field">
                @Html.TextBoxFor(m => m.Description, new { id = "txtDescription" })
                @Html.ValidationMessageFor(m => m.Description)
            </div>
           <div class="editor-label">
                @Html.Label("Upload your picture");
            </div>
            <p>
                <input type="file"  id="imagesubmit" name="imagesubmit" value="Submit"  
onclick="saveuserinfo();"/>
            </p>
}

javascript code

function saveuserinfo() 
{
        var description = document.getElementById('txtDescription').value;
        var imagefile = document.getElementById('imagesubmit');
        $.ajax({
            url: '@Url.Action("SaveuserInfo", "Welcome")',
            data: { description: description,imagesubmit: imagesubmit},
            type: 'POST',
            success: function (data) {
                            }
        });
    }

Controller code

???

I need controller code in how to get the image from javascript and pass it to DB layer for inserting

into the database.

This is what i have been trying ..Is this the right approach to store the image

public ActionResult saveuserinfo(string description,)
    {
        return View("WelcomePage");
    }

( I am stuck here with the code what datatype i have to use here to recieve the image and what

datatype i have to convert and send it to DB LAYER.

tereško
  • 58,060
  • 25
  • 98
  • 150
user2465036
  • 351
  • 2
  • 11
  • 24

2 Answers2

0

You need to add a MultifilePart parameter to your action:

 public ActionResult saveuserinfo(@RequestParam MultipartFile image) {...}

The parameter has methods that allow you to access the bytes. Note: You can read the stream only once!

Documentation: http://docs.spring.io/spring/docs/3.2.1.RELEASE/spring-framework-reference/html/mvc.html#mvc-multipart

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

You cannot upload a file using jquery ajax, or any form of plain XMLhttprequest.

You should use

@using (Html.BeginForm("YourAction", "YourController", FormMethod.Post, new { enctype = multipart/form-data" })) {
// your form elements goes here, e.g <input type="file" .....
}

Alternatively, if you really want an ajax feel over your upload, you should look at Fine Uploader: https://github.com/Widen/fine-uploader

Lars Anundskås
  • 1,345
  • 10
  • 24