qiulh

博客园 首页 新随笔 联系 订阅 管理
  2 Posts :: 0 Stories :: 1 Comments :: 0 Trackbacks

2006年5月12日 #

    当服务器要提供文件下载时,HttpResponse有这么几种方法可以使用。
1)用Response.WriteFile,如:
       

        Response.ContentType = "application/octet-stream";
        Response.WriteFile(
@"whatever.zip");

2) 采用aspnet2.0的新方法 Response.TransmitFile,注意此方法将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件。如:
     

        Response.ContentType = "application/x-zip-compressed";
        Response.AddHeader(
"Content-Disposition""attachment;filename=downloadfilename.zip");
        Response.TransmitFile(
@"whatever.zip");

(假设同文件夹下有个需要下载的文件叫whatever.zip,而用户下载时默认名称为downloadfilename.zip)
3)需要注意的是,我们都知道Server.ScriptTimeout 的默认值是90秒,而当我们在web.config中打开调试模式,此值变为30,000,000秒。这也是为什么我在开发时一般不会发现超时问题。当下载大文件时,用Response.WriteFile会使Aspnet_wp.exe缓存了太大空间而导致下载失败。
    这时建议采用文件流形式。如:
 

System.IO.Stream iStream = null;

        
//以10K为单位缓存:
        byte[] buffer = new Byte[10000];

       
int length;

       
long dataToRead;

        
// 制定文件路径.
        string filepath = @"D:\mybigfile.zip";

        
//  得到文件名.
        string filename = System.IO.Path.GetFileName(filepath);

        
try
        
{
            
// 打开文件.
            iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
                        System.IO.FileAccess.Read, System.IO.FileShare.Read);


            
// 得到文件大小:
            dataToRead = iStream.Length;

            Response.ContentType 
= "application/octet-stream";
            Response.AddHeader(
"Content-Disposition""attachment; filename="+filename);

            
while (dataToRead > 0)
            
{
                
//保证客户端连接
                if (Response.IsClientConnected)
                
{
                   length 
= iStream.Read(buffer, 010000);

                   Response.OutputStream.Write(buffer, 
0, length);

                    Response.Flush();

                    buffer 
= new Byte[10000];
                    dataToRead 
= dataToRead - length;
                }

                
else
                
{
                    
//结束循环
                    dataToRead = -1;
                }

            }

        }

        
catch (Exception ex)
        
{
            
// 出错.
            Response.Write("Error : " + ex.Message);
        }

        
finally
        
{
            
if (iStream != null)
            
{
                
//关闭文件
                iStream.Close();
            }

        }

posted @ 2007-03-29 14:46 不倒翁 阅读(368) | 评论 (0)编辑

'本控件实现附加码功能,自动产生附加码,可以调节字体大小,验证码位数。
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Public Class Wuc附加码
    
Inherits System.Web.UI.UserControl

Web 窗体设计器生成的代码
    
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
'Font_Size'字体大小 intrger
        'Char_Number'验证码的位数 intrger
        'BackgroundColor'背景颜色 十六进制数的字符串
        Dim My_Stream As MemoryStream = Get_Images(124"#ffffff")
        Show_image(My_Stream) 
'显示内存图像
        My_Stream.Close() '关闭打开的流文件

    
End Sub

    
Function Get_Images(ByVal Font_Size As IntegerByVal Char_Number As IntegerByVal BackgroundColor As StringAs MemoryStream '把字符转换为图像,并且保存到内存流
        Dim image_w As Integer = Int(Font_Size * 1.3+ Font_Size * Char_Number '这个数字在调用页面需要,你要自己算出明确的数值 注意注意注意注意!!!!!
        Dim image_h As Integer = Int(2.5 * Font_Size) '这个数字在调用页面需要,你要自己算出明确的数值    注意注意注意注意!!!!!

        
Dim Temp_Bitmap As Bitmap '封装GDI+位图 
        Dim Temp_Graphics As Graphics '封装GDI+绘图面
        Dim Color_Back As Color = ColorTranslator.FromHtml(BackgroundColor) '背景颜色 

        Temp_Bitmap 
= New Bitmap(image_w, image_h, PixelFormat.Format32bppRgb)  '注意注  确定背景大小

        Temp_Graphics 
= Graphics.FromImage(Temp_Bitmap)
        Temp_Graphics.FillRectangle(
New SolidBrush(Color_Back), New Rectangle(00, image_w, 5 * image_h)) '注意注   绘制背景 

        
'Dim Sesson_Company As String = "" '为了进行验证比较
        Dim ran As New System.Random
        
Dim codeint As Integer = ran.Next(10009999)
        
Dim code As String = CStr(codeint)

        
Me.Session.Add("code", code)
        Temp_Graphics.DrawString(code, 
New Font("Arial"16), New SolidBrush(Color.Black), 00)
Dim Temp_Stream As MemoryStream = New MemoryStream
        Temp_Bitmap.Save(Temp_Stream, ImageFormat.Jpeg)
        Temp_Graphics.Dispose() 
'释放资源
        Temp_Bitmap.Dispose() '释放资源
        Temp_Stream.Close() '关闭打开的流文件
        Return Temp_Stream '返回流
    End Function

 
Function Show_image(ByVal Show_Stream As MemoryStream) '显示内存图像
        Response.ClearContent()
        Response.ContentType 
= "Image/Jpeg"
        Response.BinaryWrite(Show_Stream.ToArray())
        Response.End()
    
End Function

End Class


posted @ 2006-05-12 20:50 不倒翁 阅读(89) | 评论 (1)编辑