public static void DownloadFile(string fileName,string showName)
{
string physicalFilePath = GetDianDanHeTongImagePath()+@"\"+fileName;
FileStream stream=null;
try
{
stream = new FileStream(physicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
int bufSize = (int)stream.Length;
byte[] buf = new byte[bufSize];
int bytesRead = stream.Read(buf, 0, bufSize);
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition","attachment; filename=\"" + HttpUtility.UrlEncode(showName,System.Text.Encoding.UTF8) + "\"");
HttpContext.Current.Response.OutputStream.Write(buf, 0, bytesRead);
HttpContext.Current.Response.End();
stream.Close();
}
finally {
}
}文件存在局网服务器下,为什么执行到
HttpContext.Current.Response.ContentType 的时候就说:未将对像引用到实例呢?
{
string physicalFilePath = GetDianDanHeTongImagePath()+@"\"+fileName;
FileStream stream=null;
try
{
stream = new FileStream(physicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
int bufSize = (int)stream.Length;
byte[] buf = new byte[bufSize];
int bytesRead = stream.Read(buf, 0, bufSize);
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition","attachment; filename=\"" + HttpUtility.UrlEncode(showName,System.Text.Encoding.UTF8) + "\"");
HttpContext.Current.Response.OutputStream.Write(buf, 0, bytesRead);
HttpContext.Current.Response.End();
stream.Close();
}
finally {
}
}文件存在局网服务器下,为什么执行到
HttpContext.Current.Response.ContentType 的时候就说:未将对像引用到实例呢?
解决方案 »
- 找不到类型或命名空间名称“SaveFileDialog”(是否缺少 using 指令或程序集引用?)
- 求.NET的面试题
- GridView的TemplateField里面如何绑定后台的字段
- C#调用VB编译的DLL,经过数次调整,在正常线程下调用并未出现问题,多线程时无规律地出现问题。
- FileSystemWatcher中能否得到访问文件的进程名
- C#报表打印问题,高手来啊!
- bmp显示问题!!!
- Asp.net2.0 web中框架与页面之间的传值?
- TextBox有回车事件么。要怎么样处理回车呢
- 能不能介绍几本关于c #开发数据库方面好一点的书籍!
- datagird新增加一行的问题
- picturebox加了hsscrollbar后,hsscrollbar没有起到作用,请问是为什么??在线等
建议你用webclient来做,它有DownloadData(path)和OpenRead(path)方法
saveFile.Filter = "*.tif|*.TIF|*.jpg|*.JPG|*.gif|*.GIF";
string SourceURL = CRM.Sys.Utility.CommonFun.GetDianDanHeTongImagePath()+@"\"+fileName;
DialogResult result = saveFile.ShowDialog();
//saveFile.
if(result ==DialogResult.Cancel)
return;
string DestPath = saveFile.FileName;
System.Net.WebClient webClient=new System.Net.WebClient();
Stream stream = webClient.OpenRead(SourceURL);
byte[] arrByte = new byte[1024];
long completedByteCount = 0;
if (File.Exists(DestPath))
File.Delete(DestPath);
FileStream fStream = new FileStream(DestPath,FileMode.CreateNew,FileAccess.Write);
while(true)
{
int readCnt = stream.Read(arrByte,0,1024);
if(readCnt==0)
break;
fStream.Write(arrByte,0,readCnt);
completedByteCount += readCnt;
}
stream.Close();
fStream.Close();///不过现在有一个小问题时,在弹出SaveFileDialog的时候,我不知如何初始化"保存文件的名字"