How to download file sharepoint 2010

There is a demand of customers need to click to download the page to download the file, according to the traditional method is to appear URI is malformed. The survey found that the traditional ways of looking at this the wrong file is reported in the fight. The original is sharepoint you put these files are saved in the form of two sub-sections into the database file uploading pictures of the time.

The need to achieve the following ways:

 1                 SPSecurity.RunWithElevatedPrivileges(delegate()
 2                 {
 3                     using (SPSite site = new SPSite(SPContext.Current.Site.ID))
 4                     {
 5                         using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
 6                         {
 7                             web.AllowUnsafeUpdates = true;
 8                             string url = img;
 9                             SPFile tempFile = web.GetFile(url);
10                             byte[] obj = (byte[])tempFile.OpenBinary();
11                             Response.Clear();
12                             Response.ClearContent();
13                             Response.ClearHeaders();
14                             Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(imgTitle.ToString()));                       
15                             if (Response.IsClientConnected)
16                             {
17                                 Response.BinaryWrite(obj);
18                             }
19                             Response.Flush();
20                             Response.Close();
21                         }
22                     }
23                 });

 

Reproduced in: https: //www.cnblogs.com/lynn-lin/archive/2013/01/18/2866721.html

Guess you like

Origin blog.csdn.net/weixin_34007886/article/details/93639893