Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用c将服务器文件下载到客户端系统#_C#_Asp.net - Fatal编程技术网

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

C# 如何使用c将服务器文件下载到客户端系统#,c#,asp.net,C#,Asp.net,我试过了,但没用, 我不知道我哪里出错了。我正在使用C#3.0使用此代码 string filePath1 = (sender as LinkButton).CommandArgument; string filepath = ("D:\\RetailAgreement\\" + filePath1); FileInfo myfile = new FileInfo(filepath); if (filePath1 != "") { Response.ClearContent();

我试过了,但没用, 我不知道我哪里出错了。我正在使用C#3.0

使用此代码

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

}

在您的webapp下移动(您要下载的文件)文件,并使用
Server.MapPath
方法获取真实路径。您如何调用此代码?您向我们展示了一个javascritp错误,但没有javascript。这是一个AJAX调用吗?如果是这样的话,您就错了,您可以像这样下载一个文件。页面标题已经设置好了,AJAX不会这样做,需要一个非常不同的响应。你不需要玩太多的反应。看看
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.");
            }
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.");
            }