如何使用C#指定客户端下载路径 [英] How to specify Clientside-Download path using C#

查看:99
本文介绍了如何使用C#指定客户端下载路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将文件下载到客户端.如何在没有用户交互的情况下以编程方式将客户端的下载路径设置为C:驱动器.

Default.aspx

I am using the following code to download a file to client. How to set the download path at clientside to C: drive programatically without user interaction.

Default.aspx

protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("download.aspx?file=~/images/aa.txt");
    }



Download.aspx



Download.aspx

protected void Page_Load(object sender, EventArgs e)
    {
        string strRequest =(string) Request.QueryString["file"];       
        if (!string.IsNullOrEmpty(strRequest))
        {
            string path = Server.MapPath(strRequest);           
            System.IO.FileInfo file = new System.IO.FileInfo(path);           
            if (file.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.WriteFile(file.FullName);
                Response.End();              
            }
            else
            {
                Response.Write("This file does not exist.");
            }           
        }
        else
        {
            Response.Write("Please provide a file to download.");
        }


    }

推荐答案

您可以尝试在filename变量中指定一个路径,但我怀疑它是否会起作用.总是会问您的用户,因为网站不能只是将文件写入本地用户,这将是一个巨大的安全漏洞.

此外,Windows 7用户无法将文件保存到c:\ my file.txt.
You might try specifying a path in the filename variable, but I doubt it will work. Your user will always be asked, because a website can''t just write a file to a local user, that would be a huge security hole.

Also, a windows 7 user cannot save files to c:\my file.txt.


这篇关于如何使用C#指定客户端下载路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆