下载文件到客户端PC在ASP.NET中使用VB.NET [英] Download file to client PC in ASP.NET using VB.NET

查看:208
本文介绍了下载文件到客户端PC在ASP.NET中使用VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个网站,将只能在我们的组织是可访问的。

I am developing a website that would be accessible only within our organisation.

我想实现一个功能:在客户端从服务器下载文件(Visio文件* .VSD),并将其保存到任何位置。

I want to implement a functionality in which the client will download a file (Visio File *.vsd) from the server and save it to any location.

我碰到一个解决方案来:

I came across a solution:

dim wc as new WebClient ()

wc.downloadFile(src,dest)

但是,这个方案不会提示另存为对话框(我想在我的应用程序)。我也应该知道客户端的PC在那里他保存的文件,以便路径可以保存在一个数据库上的路径。

However, this solution doesn't prompt the save as dialog box (which I want in my application). Also I should know the path on the client's PC where he has saved the file, so that the path can be saved in a database.

(参考:我想要实现类似功能的 VSS

(For reference: I want to implement the functionality similar to VSS.)

推荐答案

在ASP.NET,如果要传输的文件给客户,并有另存为对话框提示用户选择一个位置,你将不得不设置正确的内容类型和内容处置响应头,然后将文件直接写入响应流:

In ASP.NET if you want to stream a file to the client and have the Save As dialog prompt the user to select a location you will have to set the correct Content-Type and Content-Disposition response headers and then write the file directly to the response stream:

例如:

protected void SomeButton_Click(object sender, EventArgs e)
{
    // TODO: adjust the path to the file on the server that you want to download
    var fileToDownload = Server.MapPath("~/App_Data/someFile.pdf");

    Response.ContentType = "application/octet-stream";
    var cd = new ContentDisposition();
    cd.Inline = false;
    cd.FileName = Path.GetFileName(fileToDownload);
    Response.AppendHeader("Content-Disposition", cd.ToString());

    byte[] fileData = System.IO.File.ReadAllBytes(fileToDownload);
    Response.OutputStream.Write(fileData, 0, fileData.Length);
}

现在这个code执行时,该文件将被发送到客户端浏览器将提示保存它放在自己的电脑上的特定位置。

Now when this code executes, the file will be sent to the client browser which will prompt to Save it on a particular location on his computer.

不幸的是出于安全原因,你没有摄​​取其中客户选择存储在他的计算机上的文件目录的方式。这些信息从未在过境线,你有没有办法知道它ASP.NET应用程序内的方法。所以,你必须找到获取此信息,例如要求客户在某些文本框或其他字段中输入它的一些其他的方式。

Unfortunately for security reasons you have no way of capturing the directory in which the client choose to store the file on his computer. This information never transits over the wire and you have no way of knowing it inside your ASP.NET application. So you will have to find some other way of obtaining this information, such as for example asking the client to enter it in some text box or other field.

这篇关于下载文件到客户端PC在ASP.NET中使用VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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