Asp.net 从远程服务器下载文件

Asp.net 从远程服务器下载文件,asp.net,download,Asp.net,Download,我面临一个奇怪的问题 我有一个网页(Filelibrary.aspx)来下载一些word/pdf文件,我可以从本地机器下载文件,并从本地pc直接链接。但是我们有一个服务器,我们通常通过作为远程桌面登录服务器来访问该网站。如果我们试图从remort桌面下载word/pdf文件,则下载的是网页“Filelibrary.aspx”,而不是word/pdf。我们正在使用https。我可以下载它之前,当它是http 我的代码: String strFile = String.Empty; String[]

我面临一个奇怪的问题

我有一个网页(Filelibrary.aspx)来下载一些word/pdf文件,我可以从本地机器下载文件,并从本地pc直接链接。但是我们有一个服务器,我们通常通过作为远程桌面登录服务器来访问该网站。如果我们试图从remort桌面下载word/pdf文件,则下载的是网页“Filelibrary.aspx”,而不是word/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");
有人能帮我解决这个奇怪的问题吗?

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

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

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

        filename = strFile.Split('\\');
//添加了这两行 HttpContext.Current.Response.Cache.SetNoServerCaching()


它是从服务器下载实际的源文件
Filelibrary.aspx
,还是下载Word/PDF文件,但保存为
Filelibrary.aspx
?不,它完全是下载html内容。当我们试图在word中打开时,它显示了加载css文件的错误,那么听起来您的服务器不是在执行aspx文件,而是将其作为静态文件提供。检查服务器上是否安装了ASP.Net并在IIS中正确注册(我假设是IIS)。我尝试使用Google Chrome进行下载,但工作正常。。所有其他人只能使用IE,因为他们的安全策略。。IE中是否有任何安全设置可在用户远程登录时重新限制文件下载?这可能是因为IE在quirks模式下运行,或者因为渲染引擎未设置为最新版本。您是否将
X-UA-Compatible
设置为edge,并且是否具有有效的doctype?不过,我在抓救命稻草。也许如果你在不同的场景中加入更多的信息
        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");