看到一篇关于上传文章,先抄过来,慢慢学习

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 hsxixi 的 blog

原贴在这里:http://blog.joycode.com/saucer/archive/2004/03/16/16225.aspx

我们在上传大文件时都遇到过这样或那样的问题。设置很大的maxrequestlength 值并不能完全解决问题,因为asp.net会block直到把整个文件载入内存后,再加以处理。实际上,如果文件很大的话,我们经常会见到 internet explorer显示 "the page cannot be displayed - cannot find server or dns error",好像是怎么也catch不了这个错误。为什么?因为这是个client side错误,server side端的application_error是处理不到的,可以参考这个帖子研究一下产生这个错误的机理。

handling server error when upload file too large

解决的方法是利用隐含的httpworkerrequest,用它的getpreloadedentitybody 和 readentitybody方法从iis为asp.net建立的pipe里分块读取数据

  iserviceprovider provider = (iserviceprovider) httpcontext.current;
  httpworkerrequest wr = (httpworkerrequest) provider.getservice(typeof(httpworkerrequest));
  byte[] bs = wr.getpreloadedentitybody();
  ....
  if (!wr.isentireentitybodyispreloaded())
  {
        int n = 1024;
        byte[] bs2 = new byte[n];
        while (wr.readentitybody(bs2,n) >0)
       {
             .....
        }
  }

chris hynes为我们提供了这样的一个方案(用httpmodule),该方案除了允许你上传大文件外,还能实时显示上传进度:

asp.net upload magic part 2

这里有他讲座的ppt文件:

uploading with asp.net (part 1)

uploading with asp.net (part 2)


本文关键:看到一篇关于上传文章,先抄过来,慢慢学习
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top