Source/NFX.Wave/Response.cs
/// <summary>
/// Write the file to the client so client can download it. May set Buffered=false to use chunked encoding for big files
/// </summary>
Write the file to the client so client can download it. May set Buffered=false to use chunked encoding for big files
Write the file to the client so client can download it . May set Buffered = false to use chunked encoding for big files
public void WriteFile(string localFileName, int bufferSize = DEFAULT_DOWNLOAD_BUFFER_SIZE, bool attachment = false)
      {
        if (localFileName.IsNullOrWhiteSpace())
          throw new WaveException(StringConsts.ARGUMENT_ERROR+"Response.WriteFile(localFileName==null|empty)");
        var fi = new FileInfo(localFileName);
        if (!fi.Exists)
          throw new WaveException(StringConsts.RESPONSE_WRITE_FILE_DOES_NOT_EXIST_ERROR.Args(localFileName));
        var fsize = fi.Length;
        if (Buffered && fsize>MAX_DOWNLOAD_FILE_SIZE)
          throw new WaveException(StringConsts.RESPONSE_WRITE_FILE_OVER_MAX_SIZE_ERROR.Args(localFileName, MAX_DOWNLOAD_FILE_SIZE));
        var ext = Path.GetExtension(localFileName);
        setWasWrittenTo();
        m_NetResponse.ContentType = NFX.Web.ContentType.ExtensionToContentType(ext, NFX.Web.ContentType.BINARY);
        if (attachment)
          m_NetResponse.Headers.Add(WebConsts.HTTP_HDR_CONTENT_DISPOSITION, "attachment; filename={0}".Args(fi.Name));
        if (bufferSize<MIN_DOWNLOAD_BUFFER_SIZE) bufferSize=MIN_DOWNLOAD_BUFFER_SIZE;
        else if (bufferSize>MAX_DOWNLOAD_BUFFER_SIZE) bufferSize=MAX_DOWNLOAD_BUFFER_SIZE;
        using(var fs = new FileStream(localFileName, FileMode.Open, FileAccess.Read))
        {
          var dest = getStream();
          if (dest is MemoryStream)
          {
            var ms = ((MemoryStream)dest);
            if (ms.Position==0)
            {
              ms.SetLength(fsize);
              ms.Position = ms.Length;
              fs.Read(ms.GetBuffer(), 0, (int)ms.Position);
              return;
            }
          }
          fs.CopyTo(dest, bufferSize);
        }
      }
public void WriteFile ( string localFileName , int bufferSize = DEFAULT_DOWNLOAD_BUFFER_SIZE , bool attachment = false ) { if ( localFileName . IsNullOrWhiteSpace ( ) ) throw new WaveException ( StringConsts . ARGUMENT_ERROR + " " ) ; var fi = new FileInfo ( localFileName ) ; if ( ! fi . Exists ) throw new WaveException ( StringConsts . RESPONSE_WRITE_FILE_DOES_NOT_EXIST_ERROR . Args ( localFileName ) ) ; var fsize = fi . Length ; if ( Buffered && fsize > MAX_DOWNLOAD_FILE_SIZE ) throw new WaveException ( StringConsts . RESPONSE_WRITE_FILE_OVER_MAX_SIZE_ERROR . Args ( localFileName , MAX_DOWNLOAD_FILE_SIZE ) ) ; var ext = Path . GetExtension ( localFileName ) ; setWasWrittenTo ( ) ; m_NetResponse . ContentType = NFX . Web . ContentType . ExtensionToContentType ( ext , NFX . Web . ContentType . BINARY ) ; if ( attachment ) m_NetResponse . Headers . Add ( WebConsts . HTTP_HDR_CONTENT_DISPOSITION , " " . Args ( fi . Name ) ) ; if ( bufferSize < MIN_DOWNLOAD_BUFFER_SIZE ) bufferSize = MIN_DOWNLOAD_BUFFER_SIZE ; else if ( bufferSize > MAX_DOWNLOAD_BUFFER_SIZE ) bufferSize = MAX_DOWNLOAD_BUFFER_SIZE ; using ( var fs = new FileStream ( localFileName , FileMode . Open , FileAccess . Read ) ) { var dest = getStream ( ) ; if ( dest is MemoryStream ) { var ms = ( ( MemoryStream ) dest ) ; if ( ms . Position == 0 ) { ms . SetLength ( fsize ) ; ms . Position = ms . Length ; fs . Read ( ms . GetBuffer ( ) , 0 , ( int ) ms . Position ) ; return ; } } fs . CopyTo ( dest , bufferSize ) ; } }
Write the file to the client so client can download it.
Write the file to the client so client can download it .
//pre-allocate memory stream
localFileName
string
bufferSize
int
attachment
bool