Download File

 

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SPWeb w = SPContext.Current.Web
                siteUrl = w.Url + "/";
                string fileurl = Request.QueryString["fileurl"];

                string fileListTitle = MainList;

                if (fileurl != null && fileurl != string.Empty)
                {
                    //string url = HttpUtility.UrlDecode(fileurl, Encoding.UTF8);
                    //url = url.Replace("+", "%20");
                    string url = fileurl;

                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite site = new SPSite(url))
                        {
                            using (SPWeb ssweb = site.OpenWeb())
                            {
                                ssweb.AllowUnsafeUpdates = true;
                                SPFile file = ssweb.GetFile(fileurl);
                                if (file != null && SpecialItem(file))
                                {
                                    SPFolder folder = file.ParentFolder;
                                    SPListItem ListItem = ServerBuilder.GetListItemByField(ssweb, fileListTitle, "ItemGuid", folder.Name);
                                    if (ListItem != null)
                                    {
                                            Stream fs = file.OpenBinaryStream();
                                            byte[] buffer = new byte[fs.Length];
                                            fs.Position = 0;
                                            fs.Read(buffer, 0, (int)fs.Length);
                                            Response.Clear();
                                            Response.AddHeader("Content-Length", fs.Length.ToString());
                                            string extension = file.Item["File Type"].ToString();

                                            string fileName = HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8);
                                            fileName = fileName.Replace("+", "%20");
                                            Response.ContentType = "application/" + extension;
                                            Response.AddHeader("Content-Disposition", "attachment;FileName=" + fileName);

                                            Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

                                            fs.Close();
                                            Response.BinaryWrite(buffer);
                                            Response.OutputStream.Flush();
                                            Response.OutputStream.Close();
                                    }


                                }
                                ssweb.AllowUnsafeUpdates = false;

                            }
                        }
                    });

                }
            }
        }

 

posted @ 2018-06-21 17:13  JackHu88  阅读(209)  评论(0编辑  收藏  举报