
<add key="UploadPath" value="/Security/Uploads" />
//下载目录虚拟路径
<add key="DownloadURL" value="http://localhost/downloads" />
//windows用户名
<add key="碰携BasicAuthenticationUser" value="administrator"尘拦 />
//windows用户密码
<add key="BasicAuthenticationPWD" value="admin$123" />
3、下载程笑兄伏序源码
using System
using System.Collections
using System.ComponentModel
using System.Data
using System.Drawing
using System.Web
using System.Web.SessionState
using System.Web.UI
using System.Web.UI.WebControls
using System.Web.UI.HtmlControls
using System.Net
using System.IO
namespace security
{
/// <SUMMARY>
/// </SUMMARY>
public class SecureFile
{
public SecureFile()
{
}
public bool UploadFile(HtmlInputFile inputfile)
{
try
{
string fileName = ""
string DirPath = ""
if(( inputfile.PostedFile != null ) &&
( inputfile.PostedFile.ContentLength >0 ))
{
DirPath=HttpContext.Current.Server.MapPath(
System.Configuration.ConfigurationSettings.AppSettings[
"UploadPath"])
fileName = System.IO.Path.GetFileName(
inputfile.PostedFile.FileName )
inputfile.PostedFile.SaveAs( DirPath "\\\" fileName )
}
return true
}
catch
{
return false
}
}
public bool DownloadFile(string strFile)
{
try
{
string strDownloadURL=
System.Configuration.ConfigurationSettings.AppSettings[
"DownloadURL"]
string strUser=
System.Configuration.ConfigurationSettings.AppSettings[
"BasicAuthenticationUser"]
string strPWD=
System.Configuration.ConfigurationSettings.AppSettings[
"BasicAuthenticationPWD"]
string strURL=strDownloadURL "\\\" strFile
WebClient req=new WebClient()
CredentialCache mycache=new CredentialCache()
mycache.Add(new Uri(strURL),"Basic",
new NetworkCredential(strUser,strPWD))
req.Credentials=mycache
HttpResponse response = HttpContext.Current.Response
response.Clear()
response.ClearContent()
response.ClearHeaders()
response.Buffer= true
response.AddHeader("Content-Disposition",
"attachmentfilename=\"" strFile "\"")
byte[] data=req.DownloadData(strURL)
response.BinaryWrite(data)
response.End()
return true
}
catch(Exception ex)
{
if(ex.Message=="The remote server "
"returned an error: (404) Not Found.")
throw new Exception("File not found")
else if(ex.Message=="The remote server"
" returned an error: (401) Unauthorized.")
throw new Exception("Unauthorized access")
return false
}
}
}
}
在我们的系统的编写或磨迟过程中 应该有很多的时候需要客户下载文件 我第一次的做法(应该也是大部分人的做法吧?)是:
HttpResponse response = HttpContext Current Response string js = <script language=javascript>window open( { } )</script> js = string Format(js url) response Write(js)
但是有个问题了 就是会被广告拦截软件直接拦截掉 另我非常的头痛 于是寻找更好的解决方法 看了用Response BinaryWrite写文件流一文之后觉得确实可以如此 修改代码如下:
/**//**//**//// <summary> 衫李 /**//// 下载文件 /**//// </summary> /**//// <param name= filename >文件物理地址</param> protected void DownloadFile(string filename) { string saveFileName = test xls int intStart = filename LastIndexOf( \ )+ saveFileName = filename Substring(intStart filename Length intStart) FileStream MyFileStream long FileSize MyFileStream = new FileStream(filename FileMode Open) FileSize = MyFileStream Length byte[] Buffer = new byte[(int)FileSize] MyFileStream Read(Buffer (int)FileSize) MyFileStream Close() Response AddHeader( Content Disposition attachmentfilename= +saveFileName) Response ContentEncoding = System Text Encoding GetEncoding( GB ) Response ContentType = application/vnd ms excel Response BinaryWrite(Buffer) 游纳 Response Flush() Response Close() Response End() }
但是有个严重的问题 就是文件格式 这样只是将流输出 且无法正确识别格式 还好 能人层出不穷 柚子Nan 提出了能否不考虑文件的类型 直接把文件显示到浏览器(Response) 的想法正好切中我的要害所在 于是急忙研究了柚子Nan的想法 修改出最后代码
/**//**//**//// <summary> /**//// 下载文件 /**//// </summary> /**//// <param name= filename >文件物理地址</param> protected void DownloadFile(string filename) { string saveFileName = test xls int intStart = filename LastIndexOf( \ )+ saveFileName = filename Substring(intStart filename Length intStart) Response Clear() Response Charset = utf Response Buffer= true this EnableViewState = false Response ContentEncoding = System Text Encoding UTF Response AppendHeader( Content Disposition attachmentfilename= + saveFileName) Response WriteFile(filename) Response Flush() Response Close() Response End() }
使用昨天直接保存文件到客户端 中的方法 经过我的反复测试 各式文档都运行完全正常 于是昨晚修改了现有代码 修改了下载方法 以解决一直困扰自己的窗口拦截问题 早上本来还沾沾自喜 这下再也不用老跟客户解释为什么窗口会没掉了 可惜啊 人算不如天算 早上客户就反映下载的文件全是乱码 立马在本机进行测试 没问题 再同事的机器上试验 同样没问题 那应该是客户端的问题才是 只好让客户NetMeeting演示一下她的 *** 作过程 下载 〉保存 〉打开 这么简单的流程 不会做错吧? 正在郁闷之际 突然脑光一闪 终于发现不一样的地方 立马试验 果然如此! 到底有什么区别呢?请看 *** 作图: 客人 *** 作图 我的 *** 作图 各位应该看出不同之处了吧?还看不出来? 这件事情的罪魁祸首就是: 解决方法:使用lovecherry 的如何从注册表读取文件的ContentType 一文的方法
修正代码: /**//// <summary> /// 下载文件 /// </summary> /// <param name= filename >文件物理地址</param> protected void DownloadFile(string filename) { string saveFileName = test xls int intStart = filename LastIndexOf( \\ )+ saveFileName = filename Substring(intStart filename Length intStart) System IO FileInfo fi=new System IO FileInfo(filename) string fileextname=fi Extension string DEFAULT_CONTENT_TYPE = application/unknown RegistryKey regkey fileextkey string filecontenttype try { regkey=Registry ClassesRoot fileextkey=regkey OpenSubKey(fileextname) filecontenttype=fileextkey GetValue( Content Type DEFAULT_CONTENT_TYPE) ToString() } catch { filecontenttype=DEFAULT_CONTENT_TYPE } Response Clear() Response Charset = utf Response Buffer= true this EnableViewState = false Response ContentEncoding = System Text Encoding UTF Response AppendHeader( Content Disposition attachmentfilename= + saveFileName) Response ContentType=filecontenttype Response WriteFile(filename) Response Flush() Response Close() Response End() } 最后得出结论:要实现柚子Nan提出的能否不考虑文件的类型 直接把文件显示到浏览器(Response) 有一种方法 让客户端都不要隐藏已知的扩展名 但是这种方法是无法适应大部分电脑使用者的(一般只有比较熟悉电脑的人才会这样做吧?)
bbs 看中的方法 还没有试用 不知道有没有作用
lishixinzhi/Article/program/net/201311/11936Partial HTTP响应头用于指定在下载文件时只下载部分内容,这通常用于支持断点续传。如果您加了Partial HTTP响应头但下载文件失败了,可能是以下原因:
1. 服务器友空不支持HTTP部分响此备应:如果服务器不支持HTTP部分响应,则客户端在请求包含Partial HTTP响应头的文件时会得到一个错误响应。
2. 文件大小不匹配:如果文件大小与服务器响应的大小不匹配,则部分响应将无法正确处理,导致下载失败。
3. 网络连接问题:下载文件时遇到的网络问题可能会导致文件下载失败。您可以尝试使用另一个网络或重启路由器,然后重新下载文件。
如果以上方法无效,则建议查看服务器日志以了解更多详细信息,并联系服务器管理员森告毁进行帮助和支持。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)