如何更改下载文档的目标目录

时间:2015-01-23 09:48:07

标签: c# download document

string Idd = Convert.ToString(Page.Request.QueryString["Id"]);
    string DocName = Convert.ToString(Page.Request.QueryString["Name"]);
    #region
    try
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite osite = new SPSite(SPContext.Current.Web.Url))
                {
                    using (SPWeb web = osite.OpenWeb())
                    {
                        //LinkButton lnkView = (LinkButton)e.CommandSource;
                        //string Name = lnkView.CommandArgument;
                        //string ID = lnkView.ID;
                        SPDocumentLibrary library = web.Lists["Shared Documents"] as SPDocumentLibrary;
                        string filepath = library.RootFolder.Url;
                        string filename = DocName;
                        string IDofDoc = Idd;

                        //SPFile file = web.GetFile(library.RootFolder.Url + "/No Easy Day.pdf");
                        SPFile file = web.GetFile(filepath + "/" + filename);

                        Stream stream = file.OpenBinaryStream();
                        FileStream fileStream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
                        //string filepath1 = fileStream.Name.Replace(fileStream.Name, @"~\\SAIDI\\" + "Asset//");
                        #region
                        //string filepath1 = (sender as Button).Command;
                        //Response.ContentType = ContentType;
                        //filepath1=MapPath(@"~\\SAIDI\\" +"Asset//"+filepath);
                        //Response.AppendHeader("Content-Disposition", "attachment;filename=" + Path.GetFullPath());
                        //Response.WriteFile(filepath1);
                        //Response.End();                         
                        //duplicate
                        #endregion
                        int buffer = 4096;
                        int read = buffer;
                        byte[] bytes = new byte[buffer];
                        while (read == buffer)
                        {
                            read = stream.Read(bytes, 0, buffer);
                            fileStream.Write(bytes, 0, read);
                            if (read < buffer) break;
                        }
                        stream.Dispose();
                        fileStream.Dispose();
                    }
                }
            });
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message + "<br>" + ex.StackTrace);
    }

问题是: - 下载的文件被下载到

C:\windows\system32\inetsrv
  1. 如何更改目标文件路径?
  2. 我已将此代码放在生产服务器中,但未下载文件。
  3. 那些解决方案是什么?

3 个答案:

答案 0 :(得分:0)

string Idd = Convert.ToString(Page.Request.QueryString["Id"]);
    string DocName = Convert.ToString(Page.Request.QueryString["Name"]);
    #region
    try
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite osite = new SPSite(SPContext.Current.Web.Url))
                {
                    using (SPWeb web = osite.OpenWeb())
                    {
                        //LinkButton lnkView = (LinkButton)e.CommandSource;
                        //string Name = lnkView.CommandArgument;
                        //string ID = lnkView.ID;
                        SPDocumentLibrary library = web.Lists["Shared Documents"] as SPDocumentLibrary;
                        string filepath = library.RootFolder.Url;
                        string filename = DocName;
                        string IDofDoc = Idd;

                        //SPFile file = web.GetFile(library.RootFolder.Url + "/No Easy Day.pdf");
                        SPFile file = web.GetFile(filepath + "/" + filename);

                        Stream stream = file.OpenBinaryStream();

                        filename= Path.Combine(filepath, filename);

                        FileStream fileStream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
                        //string filepath1 = fileStream.Name.Replace(fileStream.Name, @"~\\SAIDI\\" + "Asset//");
                        #region
                        //string filepath1 = (sender as Button).Command;
                        //Response.ContentType = ContentType;
                        //filepath1=MapPath(@"~\\SAIDI\\" +"Asset//"+filepath);
                        //Response.AppendHeader("Content-Disposition", "attachment;filename=" + Path.GetFullPath());
                        //Response.WriteFile(filepath1);
                        //Response.End();                         
                        //duplicate
                        #endregion
                        int buffer = 4096;
                        int read = buffer;
                        byte[] bytes = new byte[buffer];
                        while (read == buffer)
                        {
                            read = stream.Read(bytes, 0, buffer);
                            fileStream.Write(bytes, 0, read);
                            if (read < buffer) break;
                        }
                        stream.Dispose();
                        fileStream.Dispose();
                    }
                }
            });
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message + "<br>" + ex.StackTrace);
    }

答案 1 :(得分:0)

回答第二个问题:using语句是有效的try / catch-block(与处理对象有关的一些额外逻辑)。我建议用显式的try / catch-blocks替换使用。这样你就可以捕获exeptions并将它们记录到一个文件(或任何必要的东西)。

的Sascha

答案 2 :(得分:-1)

1)如何更改目标文件路径。?

您可以将目标路径作为FileStream constructor

中的第一个参数传递

例如:

FileStream fileStream = new FileStream("D:\\" + filename, FileMode.OpenOrCreate, FileAccess.Write);

如果您想使用公共位置,可以使用Environment.SpecialFolder

示例:

FileStream fileStream = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + filename, FileMode.OpenOrCreate, FileAccess.Write);

2)我已将此代码放在生产服务器中,但是,文件未下载

我想这是一个许可问题。尝试将文件写入另一个目录?