Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to upload and save file into sql server database... the file is uploaded and is saved into the database....
i have done this i Create Action of the controller.. the code is given below....
it shows the Content Type and name of the file on the web page...
Now i want to download the file from database and want to display the contents of file on the web page.... how to do it... can anyone help me with this...
i am doing it in asp.net mvc...
Posted
Updated 20-Oct-15 3:05am
v2
Comments
Nathan Minier 19-Oct-15 8:49am    
You'd likely be best off generating the image from BLOB on the javascript side, and assigning it to the element. Using the SRC tag like that won't work.

There's a few different approaches at:
http://stackoverflow.com/questions/7650587/using-javascript-to-display-blob
Member 10740412 19-Oct-15 8:58am    
I have tried this......
<pre>


And provide link in Index page.....
@Html.ActionLink("Download", "Download", "Advertisement")

its giving me error....how to remove this error??? at line 66
Object reference not set to an instance of an object.


Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
ORS.ControlPanel.Controllers.AdvertisementController.Download(HttpPostedFileBase file) in f:\Projects\Online Recruitment System\ORS.ControlPanel\Controllers\AdvertisementController.cs:66
lambda_method(Closure , ControllerBase , Object[] ) +138
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +228
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +34
System.Web.Mvc.Async.AsyncControllerActionInvoker.<begininvokesynchronousactionmethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +38
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +41
System.Web.Mvc.Async.AsyncInvocationWithFilters.<invokeactionmethodfilterasynchronouslyrecursive>b__3d() +71
System.Web.Mvc.Async.<>c__DisplayClass46.<invokeactionmethodfilterasynchronouslyrecursive>b__3f() +386
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.<>c__DisplayClass2b.<begininvokeaction>b__1c() +38
System.Web.Mvc.Async.<>c__DisplayClass21.<begininvokeaction>b__1e(IAsyncResult asyncResult) +186
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38
System.Web.Mvc.Controller.<beginexecutecore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +67
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38
System.Web.Mvc.MvcHandler.<beginprocessrequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +44
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +67
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +399
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +137
Nathan Minier 19-Oct-15 9:19am    
Well, it doesn't help that you never actually assign the file InputStream to add.DocImage (right now it's an array of nulls).

Without knowing what line the error is occuring on, most likely there's an error in the construction of your AdvertisementDTO object.

Edit: With the stack trace, it's clear that the file object isn't being passed correctly.
Member 10740412 19-Oct-15 10:09am    
then how to do it...
can u give any hint
Nathan Minier 19-Oct-15 10:14am    
Sure. You need to use a form to upload the file, and make sure to use the proper enctype:
http://stackoverflow.com/questions/21998637/mvc-httppostedfilebase-always-null

1 solution

HTML
<div id="UploadSection">       
    <% using (Html.BeginForm("fileUpload", "home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {%><br />
        <p><input type="file" name="fileUpload1" /> </p>
        <p><input type="submit" value="Upload file" /></p>       
    <% } %>   
</div>

public ActionResult FileUpload(HttpPostedFileBase Files)
   {
       //create object of LINQ to SQL class
       DBContext dataContext = new DBContext();
       //loop through request file collection
       foreach (string upload in Request.Files)
       {
           //create byte array of size equal to file input stream
           byte[] fileData = new byte[Request.Files[upload].InputStream.Length];
           //add file input stream into byte array
           Request.Files[upload].InputStream.Read(fileData, 0, Convert.ToInt32(Request.Files[upload].InputStream.Length));
           //create system.data.linq object using byte array
           System.Data.Linq.Binary binaryFile = new System.Data.Linq.Binary(fileData);
           //initialise object of FileDump LINQ to sql class passing values to be inserted
           FileDump record = new FileDump { FileData = binaryFile, FileName = System.IO.Path.GetFileName(Request.Files[upload].FileName) };
           //call InsertOnsubmit method to pass new object to entity
           dataContext.FileDumps.InsertOnSubmit(record);
           //call submitChanges method to execute implement changes into database
           dataContext.SubmitChanges();
       }
       var returnData = dataContext.FileDumps;
       ViewData.Model = returnData.ToList();
       return View();
   }

public FileContentResult FileDownload(int id)
   {
       //declare byte array to get file content from database and string to store file name
       byte[] fileData;
       string fileName;
       //create object of LINQ to SQL class
       DBContext dataContext = new DBContext();
       //using LINQ expression to get record from database for given id value
       var record = from p in dataContext.FileDumps
                    where p.ID == id
                    select p;
       //only one record will be returned from database as expression uses condtion on primary field
       //so get first record from returned values and retrive file content (binary) and filename
       fileData = (byte[])record.First().FileData.ToArray();
       fileName = record.First().FileName;
       //return file and provide byte file content and file name
       return File(fileData, "text", fileName);
   }


Ripped from here[^]

-KR
 
Share this answer
 

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