如何让客户端选择文件目录和名称以将文件保存在ASP.NET webform中? [英] How do I let client to choose file directory and name to save the file in ASP.NET webform?

查看:528
本文介绍了如何让客户端选择文件目录和名称以将文件保存在ASP.NET webform中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have already created a dll which has function that loads dataset to xls file.
I am trying to use the same dll for the task. Function takes two parameters, filePath with file name and dataset, and it does is create xlsx file 
in the destination with the data. 
Now i have to let user to decide to whereever they want to create (save) that xls file in their machine.

How could i achieve this task in asp.net web form.


Please help.





我的尝试:





What I have tried:

My code is something like this:


 protected void btnExport_Click(object sender, EventArgs e)
 {
DataSet dataSet = new DataSet();
//code to get data from database
sqlDataAdapter.Fill(dataSet);

string fullFileName = filePath + fileName;
ExcelUtility.ExportDataSetToExcel(fullFileName, dataSet);
}

I want user to set fullFileName. How do i do this?

推荐答案

你没有。您没有任何访问客户端计算机的权限,也无法直接在其上存储任何内容 - 您必须将文件下载到用户,然后他们决定如何处理它。 C#代码总是在服务器上运行,永远不在客户端上运行,并且只能访问服务器资源。



并试图让用户决定你的路径服务器是愚蠢的 - 而且很危险 - 他们(实际上你)不知道生产机器中允许存储文件的位置,除了相对于网站的根目录。而且你不知道那是什么,因为它没有在文件系统中修复 - 这就是为什么你有Server.MapPath将相对路径转换为文件I / O函数的正确路径。



这似乎可以在开发中使用,但这是因为客户端和服务器在同一台计算机上运行。在生产中,两者可以分开数千英里,而服务器根本无法访问客户端文件系统。
You don't. You don't have any access to the client machine, and can't store anything on there directly - you have to download the file to the user and they then decides what to do with it. C# code always runs on the Server, never on the Client, and can only ever access Server resources.

And trying to get the user to decide on a path on your server is silly - and dangerous - they (and indeed you) have no idea where in the production machine you are permitted to store files, except relative to the root directory of your website. And you don;t know what that is because it's not fixed in the file system - that's why you have Server.MapPath to convert a relative path to a "proper" path for File I/O functions.

This may appear to work in development but that's because the Client and Server are running on the same computer. In production the two can be separated by thousands of miles, and the Server has no access at all to the client file system.


这篇关于如何让客户端选择文件目录和名称以将文件保存在ASP.NET webform中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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