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

How to change the destination directory of a downloaded 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);
    }

问题是:- Downloaded Document is downloaded to

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

解决这些问题的方法是什么?

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) 我已将此代码放在生产服务器中,但是文件未下载

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

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

回答你的第二个问题:using 语句有效 try/catch-block(带有一些与处理对象相关的附加逻辑)。我建议用显式 try/catch-blocks 替换 usings。这样您就可以捕获异常并将它们记录到文件中(或任何必要的文件)。

萨沙