将文件存储在 ASP.net 网站中,然后下载到浏览器
我正在我的 asp.net 网站上用 C# 创建一个 Excel 文件。然后,我想将该文件保存在 Web 服务器文件中的某个位置,并将该文件发送到浏览器以供用户下载。
我已经让它在开发环境中的本地系统上运行,但我没有得到正确的文件寻址。
- 如何将文件存储在“~\ParentFolder\SubFolder\file.ext”中。
- 然后将该文件发送到浏览器以供用户下载。
任何指导将不胜感激。
String outputFile = Utilities.writeToExcelFile("", MapPath(@"~\ReportFiles\TempFiles\DropShipData.xls"), table);
DownloadFile(outputFile);
public void DownloadFile(string fname)
{
string path = fname;
string name = Path.GetFileName(path);
string ext = Path.GetExtension(path);
string type = "";
// set known types based on file extension
if (ext != null)
{
switch (ext.ToLower())
{
case ".htm":
case ".html":
case ".aspx":
case ".asp":
type = "text/HTML";
break;
case ".xls":
case ".xlsx":
case ".csv":
type = "Application/x-msexcel";
break;
case ".pdf":
type = "Application/pdf";
break;
case ".txt":
type = "text/plain";
break;
case ".doc":
case ".docx":
case ".rtf":
type = "Application/msword";
break;
}
}
Response.AppendHeader("content-disposition",
"attachment; filename=" + name);
if (type != "")
Response.ContentType = type;
Response.WriteFile(path);
Response.End();
}
同样,这在我的本地电脑上运行良好,但是当我将其移动到服务器时,我收到访问路径的错误。错误代码中列出的路径不是我想要文件所在的位置。
I'm creating an Excel file in C# on my asp.net web site. Then, I want to save this file somewhere within the web server's files, and send that file to the browser for the user to download.
I've got it working on my local system in the dev environment, but I'm not getting the file addressing right.
- How can I store a file in "~\ParentFolder\SubFolder\file.ext".
- Then send that file to the browser for user download.
Any guidance would be much appreciated.
String outputFile = Utilities.writeToExcelFile("", MapPath(@"~\ReportFiles\TempFiles\DropShipData.xls"), table);
DownloadFile(outputFile);
public void DownloadFile(string fname)
{
string path = fname;
string name = Path.GetFileName(path);
string ext = Path.GetExtension(path);
string type = "";
// set known types based on file extension
if (ext != null)
{
switch (ext.ToLower())
{
case ".htm":
case ".html":
case ".aspx":
case ".asp":
type = "text/HTML";
break;
case ".xls":
case ".xlsx":
case ".csv":
type = "Application/x-msexcel";
break;
case ".pdf":
type = "Application/pdf";
break;
case ".txt":
type = "text/plain";
break;
case ".doc":
case ".docx":
case ".rtf":
type = "Application/msword";
break;
}
}
Response.AppendHeader("content-disposition",
"attachment; filename=" + name);
if (type != "")
Response.ContentType = type;
Response.WriteFile(path);
Response.End();
}
Again, this works fine on my local pc, but when I move it to the server I get an error for accessing the path. And the path listed in the error code is NOT where I want the file to go.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先存储文件:
然后您可以告诉浏览器重定向到该文件
First store the file:
Then you could for example, tell the browser to redirect to that file
您可能还需要检查并确保运行 ASP.Net 辅助进程的帐户或相应的用户(如果您使用模拟)具有对 ReportFiles\TempFiles 位置的写入权限。很多时候会发生错误,因为默认权限只授予对网站中文件夹的读取权限。
You might also want to check and make sure that the account running the ASP.Net worker process, or the appropriate user if you're using Impersonation, has write privileges to the ReportFiles\TempFiles location. A lot of times the error occurs because the default privileges only give read access to the folders in your website.