文件从远程服务器下载

时间:2014-02-20 06:59:10

标签: asp.net download

我面临一个奇怪的问题。

我有一个网页(Filelibrary.aspx)下载一些word / pdf文件,我可以从本地机器下载文件,并从我的本地电脑直接链接。但是我们有一台服务器,我们通常以远程桌面的身份登录服务器来访问该站点。如果我们尝试从remort桌面下载word / pdf文件,则会下载网页“Filelibrary.aspx”而不是单词/ pdf。我们正在使用https。我可以在http之前下载它。

我的代码:

String strFile = String.Empty;
String[] filename;

strFile = Server.MapPath(ConfigurationManager.AppSettings["TemplatePath"].ToString()) + FileName;

filename = strFile.Split('\\');

Response.Clear();
Response.Buffer = true;// Read the original file from diskFileStream *
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename[filename.Length-1]);

String ext = String.Empty;
if (strFile.IndexOf(".") > 0)
    ext = (strFile.Split('.'))[1];
if (ext.ToLower() == "txt")
    Response.ContentType = "text/html";
if (ext.ToLower() == "xls")
    Response.ContentType = "xls/html";
if (ext.ToLower() == "pdf")
    Response.ContentType = "application/pdf"; 

////combine the path and file name
if (File.Exists(strFile))//open the file and process it
{
    FileStream sourceFile = new FileStream(strFile, FileMode.Open, FileAccess.Read);
    long FileSize;
    FileSize = sourceFile.Length;
    Response.AddHeader("Content-Length", FileSize.ToString()); //*
    byte[] getContent = new byte[(int)FileSize];
    sourceFile.Read(getContent, 0, (int)FileSize);

    sourceFile.Close();
    Response.BinaryWrite(getContent);
}
Common.UserPageAudit(Session["User"].ToString(), "Download Templates", Session["ROLE"].ToString(), strFile + " Template downloaded");

任何人都可以帮我解决这个奇怪的问题..?

1 个答案:

答案 0 :(得分:0)

谢谢你的朋友...... 最后我发现了问题。 我没有清除标题..我已经改变了我的代码如下。

            String strFile = String.Empty;
        String[] filename;

        strFile = Server.MapPath(ConfigurationManager.AppSettings["TemplatePath"].ToString()) + FileName;

        filename = strFile.Split('\\');

//添加了这两行             HttpContext.Current.Response.Cache.SetNoServerCaching();

        Response.ClearHeaders();

        Response.Clear();
        Response.Buffer = true;// Read the original file from diskFileStream *
        Response.AddHeader("Content-Disposition", "attachment; filename=" + filename[filename.Length-1]);


        String ext = String.Empty;
        if (strFile.IndexOf(".") > 0)
            ext = (strFile.Split('.'))[1];

        if (ext.ToLower() == "txt")
            Response.ContentType = "text/html";
        if (ext.ToLower() == "xls")
            Response.ContentType = "xls/html";
        if (ext.ToLower() == "pdf")
            Response.ContentType = "application/pdf"; 

        ////combine the path and file name
        if (File.Exists(strFile))//open the file and process it
        {
            FileStream sourceFile = new FileStream(strFile, FileMode.Open, FileAccess.Read);
            long FileSize;
            FileSize = sourceFile.Length;
            Response.AddHeader("Content-Length", FileSize.ToString()); //*
            byte[] getContent = new byte[(int)FileSize];
            sourceFile.Read(getContent, 0, (int)FileSize);

            sourceFile.Close();
            Response.BinaryWrite(getContent);
        }
        Common.UserPageAudit(Session["User"].ToString(), "Download Templates", Session["ROLE"].ToString(), strFile + " Template downloaded");