Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created an asp.net mvc 4 web application in framework 4.5 using visual studio 2012. In this i have created a database which stores the items that i want offer my clients. I want to allow my users to upload their products details too for sale. For this,
i)I want them to enter the details of their products along with its image.
ii)i want to store the images uploaded in a folder inside my project.
iii) i want to store the location of this image in the database

or if their is any other way to store images in the database that are uploaded by users. Remember it is not a website, it is a web application of MVC4
Thanks in advance
Posted
Comments
Kornfeld Eliyahu Peter 21-Jun-15 7:20am    
What have you tried?[^]
(I'm also interested in your definition for the difference between web-site and web-application...)
Kornfeld Eliyahu Peter 21-Jun-15 7:33am    
My point was as for the question you have here...
Member 10791876 21-Jun-15 7:36am    
Web application is a kind of web site or a part of any web site that does something other than displaying the content
Member 10791876 21-Jun-15 7:38am    
I m a new user here so i dont know how to show u the images of whatever i did to acheive mytarget
Kornfeld Eliyahu Peter 21-Jun-15 8:02am    
We need no images - most people here read code better than romans in their native language...So share relevant code...

1 solution

Firstly, in your .cshtml file use a file controler

C#
@using (Html.BeginForm("UploadFile", "Upload", FormMethod.Post, new {enctype = "multipart/form-data"}))
{
      @Html.AntiForgeryToken()
      <div class="form-group">
           <input type="file" id="fileToUpload" name="file" />
           <span class="field-validation-error" id="spanfile"></span>
      </div>
      <div class="form-group">
           <input type="submit" value="Upload" class="btn btn-info" id="UploadFile" name="action" />
      </div>
} 


now in your controller validate the file, get the byte array and store it n the db.

SQL
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> UploadFile(HttpPostedFileBase file, CustomModel model)
{
    if (ModelState.IsValid)
    {
        if (file == null)
        {
            ModelState.AddModelError(string.Empty, "please upload a file");
        }
        else if (file.ContentLength > 0)
        {
             // check if its a valid image type

             // get the byte array
             byte[] byteArray = file.ToByteArray();
             // Store this byte array in db
        }
    }

    return View(model);
}
 
Share this answer
 
v3
Comments
Member 10791876 24-Jun-15 3:08am    
In line, public async Task<actionresult> what would be the "Task" in respect of my project???
Shashank Bisen 24-Jun-15 3:18am    
you can use, public ActionResult UploadFile(HttpPostedFileBase file, CustomModel model)
Member 10791876 25-Jun-15 2:44am    
ok i will try

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900