使用linq使用asp.net mvc从sql server数据库上传和下载文件 [英] upload and download file to and from a sql server database using asp.net mvc using linq

查看:105
本文介绍了使用linq使用asp.net mvc从sql server数据库上传和下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件上传并保存到sql server数据库中...文件已上传并保存到数据库中....

i已完成此操作我创建控制器的动作..下面给出了代码....

它显示了网页上文件的内容类型和名称...

现在我想从下载文件数据库,并希望在网页上显示文件的内容....怎么做...任何人都可以帮助我...

i我在asp.net mvc做它。 ..

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...

推荐答案

<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);
   }





摘自 here [ ^ ]



-KR



Ripped from here[^]

-KR


这篇关于使用linq使用asp.net mvc从sql server数据库上传和下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆