本文介绍了不是有效的虚拟路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的网络应用程序中下载文件时出现上述错误。
我尝试过:
Getting above error while downloading file in my web application.
What I have tried:
string filepathforapp = "http://test.b4live.com" + link.Text.ToString();
string filePath = filepathforapp;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
推荐答案
string filePath = Request.MapPath(link.Text, Request.ApplicationPath, false);
// TODO: Validate that the file path is within the downloads folder.
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
注意评论:您需要确保发送的文件是您希望用户能够下载的。否则,您最终可能会发送 web.config
文件的内容或您网站的源代码。
Pay attention to the comment: You need to make sure that the file you're sending is one you want the user to be able to download. Otherwise, you could end up sending the contents of your web.config
file, or the source code for your site.
这篇关于不是有效的虚拟路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!