如何使用 c# 将服务器文件下载到客户端系统

本文关键字:客户 客户端 端系统 文件下载 服务器 何使用 | 更新日期: 2023-09-27 18:29:14

string filePath1 = (sender as LinkButton).CommandArgument;
string filepath = ("D:''RetailAgreement''" + filePath1);
FileInfo myfile = new FileInfo(filepath);
if (filePath1 != "")
{
    Response.ClearContent();
    Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
    Response.AddHeader("Content-Length", myfile.Length.ToString());
    Response.ContentType = ReturnExtension(myfile.Extension.ToLower());
    Response.TransmitFile(myfile.FullName);
    Response.End();
}

我试过这样,但它不起作用,我不知道我哪里做错了。我正在使用 C#3.0

如何使用 c# 将服务器文件下载到客户端系统

File.Copy("D:''RetailAgreement''",Server.Mapth("YourAplicationPath''MyFiles"), true);
//Copy Files to Your Application Path
string path = Server.MapPath("~'MyFiles" + filePath1");
            System.IO.FileInfo file = new System.IO.FileInfo(path);
            string Outgoingfile = "myfile.xlsx";
            if (file.Exists)
            {
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Outgoingfile);
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.WriteFile(file.FullName);
                Response.Flush();
                Response.Close();
            }
            else
            {
                Response.Write("This file does not exist.");
            }

使用此代码。

string path = Server.MapPath("~/DownloadedExcelFilesOp4/myfile.xlsx");
            System.IO.FileInfo file = new System.IO.FileInfo(path);
            string Outgoingfile = "myfile.xlsx";
            if (file.Exists)
            {
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Outgoingfile);
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.WriteFile(file.FullName);
                Response.Flush();
                Response.Close();
            }
            else
            {
                Response.Write("This file does not exist.");
            }