asp.net 可以这样输出文件吗?比如:string test = "输出";然后把这个变量保存到内存中,最后写成一个文件(如1.txt)输出,实现客户端下载注意:
1.这个文件(1.txt)不是来自硬盘上的或其它媒介上的。
2.不能先通过流在硬盘上建立1.txt然后再输出
3.最好能做到页面不刷新和跳转。谢谢

解决方案 »

  1.   


    public static void DownloadSmallFiles(HttpResponse _Response, string Vpath, string FielName)
        {
            _Response.Clear();
            _Response.Buffer = true;
            _Response.Charset = "utf-8";
            _Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FielName, System.Text.Encoding.UTF8));
            _Response.ContentType = "application/octet-stream";
            //_Response.ContentType = "text/plain";//注意,这两个换着试下看哪个可以。
            _Response.WriteFile(Vpath);
            _Response.Flush();
            _Response.End();
        }只可以以下载的方式让用户选择路径,你是没有权限直接写用户的硬盘的。
    另外,附HTTP MIME type 表:
    扩展名   类型/子类型   IIS   4.0   IIS   5.0     
      acx   application/internet-property-stream   否   是    
      ai   application/postscript   是   是    
      aif   audio/x-aiff   是   是    
      aifc   audio/x-aiff   是   是    
      aiff   audio/x-aiff   是   是    
      asf   video/x-ms-asf   否   是    
      asr   video/x-ms-asf   否   是    
      asx   video/x-ms-asf   否   是    
      au   audio/basic   是   是    
      avi   video/x-msvideo   是   是    
      axs   application/olescript   否   是    
      bas   text/plain   是   是    
      bcpio   application/x-bcpio   是   是    
      bin   application/octet-stream   是   是    
      bmp   image/bmp   是   是    
      c   text/plain   是   是    
      cat   application/vnd.ms-pkiseccat   否   是    
      cdf   application/x-cdf   否   是    
      cer   application/x-x509-ca-cert   否   是    
      class   application/octet-stream   是   是    
      clp   application/x-msclip   是   是    
      cmx   image/x-cmx   是   是    
      cod   image/cis-cod   是   是    
      cpio   application/x-cpio   是   是    
      crd   application/x-mscardfile   是   是    
      crl   application/pkix-crl   否   是    
      crt   application/x-x509-ca-cert   否   是    
      csh   application/x-csh   是   是    
      css   text/css   否   是    
      dcr   application/x-director   是   是    
      der   application/x-x509-ca-cert   否   是    
      dir   application/x-director   是   是    
      dll   application/x-msdownload   否   是    
      dms   application/octet-stream   是   是    
      doc   application/msword   是   是    
      dot   application/msword   是   是    
      dvi   application/x-dvi   是   是    
      dxr   application/x-director   是   是    
      eps   application/postscript   是   是    
      etx   text/x-setext   是   是    
      evy   application/envoy   是   是    
      exe   application/octet-stream   是   是    
      fif   application/fractals   否   是    
      flr   x-world/x-vrml   是   是    
      gif   image/gif   是   是    
      gtar   application/x-gtar   是   是    
      gz   application/x-gzip   否   是    
      h   text/plain   是   是    
      hdf   application/x-hdf   是   是    
      hlp   application/winhlp   是   是    
      hqx   application/mac-binhex40   是   是    
      hta   application/hta   否   是    
      htc   text/x-component   否   是    
      htm   text/html   是   是    
      html   text/html   是   是    
      htt   text/webviewhtml   否   是    
      ico   image/x-icon   否   是    
      ief   image/ief   是   是    
      iii   application/x-iphone   否   是    
      ins   application/x-internet-signup   否   是    
      isp   application/x-internet-signup   否   是    
      jfif   image/pipeg   否   是    
      jpe   image/jpeg   是   是    
      jpeg   image/jpeg   是   是    
      jpg   image/jpeg   是   是    
      js   application/x-javascript   是   是    
      latex   application/x-latex   是   是    
      lha   application/octet-stream   是   是    
      lsf   video/x-la-asf   否   是    
      lsx   video/x-la-asf   否   是    
      lzh   application/octet-stream   是   是    
      m13   application/x-msmediaview   是   是    
      m14   application/x-msmediaview   是   是    
      m3u   audio/x-mpegurl   否   是    
      man   application/x-troff-man   是   是    
      mdb   application/x-msaccess   是   是    
      me   application/x-troff-me   是   是    
      mht   message/rfc822   否   是    
      mhtml   message/rfc822   否   是    
      mid   audio/mid   否   是    
      mny   application/x-msmoney   是   是    
      mov   video/quicktime   是   是    
      movie   video/x-sgi-movie   是   是    
      mp2   video/mpeg   是   是    
      mp3   audio/mpeg   否   是    
      mpa   video/mpeg   是   是    
      mpe   video/mpeg   是   是    
      mpeg   video/mpeg   是   是    
      mpg   video/mpeg   是   是    
      mpp   application/vnd.ms-project   是   是    
      mpv2   video/mpeg   是   是    
      ms   application/x-troff-ms   是   是    
      mvb   application/x-msmediaview   是   是    
      nws   message/rfc822   否   是    
      oda   application/oda   是   是    
      p10   application/pkcs10   否   是    
      p12   application/x-pkcs12   否   是    
      p7b   application/x-pkcs7-certificates   否   是    
      p7c   application/x-pkcs7-mime   否   是    
      p7m   application/x-pkcs7-mime   否   是    
      p7r   application/x-pkcs7-certreqresp   否   是    
      p7s   application/x-pkcs7-signature   否   是    
      pbm   image/x-portable-bitmap   是   是    
      pdf   application/pdf   是   是    
      pfx   application/x-pkcs12   否   是    
      pgm   image/x-portable-graymap   是   是    
      pko   application/ynd.ms-pkipko   否   是    
      pma   application/x-perfmon   是   是    
      pmc   application/x-perfmon   是   是    
      pml   application/x-perfmon   是   是    
      pmr   application/x-perfmon   是   是    
      pmw   application/x-perfmon   是   是    
      pnm   image/x-portable-anymap   是   是    
      pot,   application/vnd.ms-powerpoint   是   是    
      ppm   image/x-portable-pixmap   是   是    
      pps   application/vnd.ms-powerpoint   是   是    
      ppt   application/vnd.ms-powerpoint   是   是    
      prf   application/pics-rules   否   是    
      ps   application/postscript   是   是    
      pub   application/x-mspublisher   是   是    
      qt   video/quicktime   是   是    
      ra   audio/x-pn-realaudio   是   是    
      ram   audio/x-pn-realaudio   是   是    
      ras   image/x-cmu-raster   是   是    
      rgb   image/x-rgb   是   是    
      rmi   audio/mid   否   是    
      roff   application/x-troff   是   是    
      rtf   application/rtf   是   是    
      rtx   text/richtext   是   是    
      scd   application/x-msschedule   是   是    
      sct   text/scriptlet   否   是    
      setpay   application/set-payment-initiation   否   是    
      setreg   application/set-registration-initiation   否   是    
      sh   application/x-sh   是   是    
      shar   application/x-shar   是   是    
      sit   application/x-stuffit   否   是    
      snd   audio/basic   是   是    
      spc   application/x-pkcs7-certificates   否   是    
      spl   application/futuresplash   否   是    
      src   application/x-wais-source   是   是    
      sst   application/vnd.ms-pkicertstore   否   是    
      stl   application/vnd.ms-pkistl   否   是    
      stm   text/html   是   是    
      sv4cpio   application/x-sv4cpio   是   是    
      sv4crc   application/x-sv4crc   是   是    
      t   application/x-troff   是   是    
      tar   application/x-tar   是   是    
      tcl   application/x-tcl   是   是    
      tex   application/x-tex   是   是    
      texi   application/x-texinfo   是   是    
      texinfo   application/x-texinfo   是   是    
      tgz   application/x-compressed   否   是    
      tif   image/tiff   是   是    
      tiff   image/tiff   是   是    
      tr   application/x-troff   是   是    
      trm   application/x-msterminal   是   是    
      tsv   text/tab-separated-values   是   是    
      txt   text/plain   是   是    
      uls   text/iuls   否   是    
      ustar   application/x-ustar   是   是    
      vcf   text/x-vcard   否   是    
      vrml   x-world/x-vrml   是   是    
      wav   audio/x-wav   是   是    
      wcm   application/vnd.ms-works   是   是    
      wdb   application/vnd.ms-works   是   是    
      wks   application/vnd.ms-works   是   是    
      wmf   application/x-msmetafile   是   是    
      wps   application/vnd.ms-works   是   是    
      wri   application/x-mswrite   是   是    
      wrl   x-world/x-vrml   是   是    
      wrz   x-world/x-vrml   是   是    
      xaf   x-world/x-vrml   是   是    
      xbm   image/x-xbitmap   是   是    
      xla   application/vnd.ms-excel   是   是    
      xlc   application/vnd.ms-excel   是   是    
      xlm   application/vnd.ms-excel   是   是    
      xls   application/vnd.ms-excel   是   是    
      xlt   application/vnd.ms-excel   是   是    
      xlw   application/vnd.ms-excel   是   是    
      xof   x-world/x-vrml   是   是    
      xpm   image/x-xpixmap   是   是    
      xwd   image/x-xwindowdump   是   是    
      z   application/x-compress   否   是    
      zip   application/zip   是   是     
      

  2.   

    string fileName ="";
    Response.Clear();   
    Response.ClearContent();   
    Response.ClearHeaders();   
    Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);   
    Response.AddHeader("Content-Length", Length.ToString());     
    Response.AddHeader("Content-Transfer-Encoding", "binary");      
    Response.ContentType = "application/octet-stream";   
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");   
    Response.WriteFile("");   
    Response.Flush();   
    Response.End();  
    string str="";
    byte[] bytes = System.Text.Encoding.Default.GetBytes(str)
    Response.ContentType = "application/octet-stream";       
    Response.AddHeader("Content-Disposition", "attachment;   filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();