SqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
Response.Clear();
Response.BinaryWrite((byte[])dr["MyFile"]);
}
dr.Close();
myconn.Close();
  }
如果我上传的是图片这样可以浏览,如果是文件我如何下载呢。

解决方案 »

  1.   

    public static void DownLoadFile(string srcFile)
    {
    HttpContext context = HttpContext.Current;
    Byte[] buffer = new Byte[10000];
    int ReadLength;
    long TotleLength;
    Stream Fstream = null;

    try
    {
    if(!File.Exists(srcFile))
    throw new Exception("file were not found."); string filename = Path.GetFileName(srcFile); Fstream = new FileStream(srcFile,
    FileMode.Open,
    FileAccess.Read,
    FileShare.Read);
    TotleLength = Fstream.Length;
    context.Response.ContentType = "Application/octec-stream";
    context.Response.AddHeader("Content-Disposition",String.Concat("Attachment;filename=",HttpUtility.UrlEncode(filename)));                long p = 0;
                   if(context.Request.Headers["Range"]!=null)
                   {
                       context.Response.StatusCode = 206;
                        p = long.Parse(context.Request.Headers["Range"].Replace("bytes=","").Replace("-",""));
                    }
                    if(p != 0)
                     {
                        context.Response.AddHeader("Content-Range","bytes " + p.ToString() + "-" + ((long)(TotleLength - 1)).ToString() + "/" + TotleLength.ToString());                    
                     }
    context.Response.AddHeader("Content-Length",((long)(TotleLength-p)).ToString());
                     Fstream.Position = p;
                     TotleLength = TotleLength - p; while(TotleLength > 0)
    {
    if(context.Response.IsClientConnected)
    {
    ReadLength = Fstream.Read(buffer,0,10000);
    context.Response.OutputStream.Write(buffer,0,ReadLength);
    context.Response.Flush();
    buffer = new Byte[10000];
    TotleLength -= ReadLength;
    }
    else
    {
    TotleLength = -1;
    }
    }
    }
    catch(Exception ex)
    {
    FileExplorer.FileError.ShowAlertMessage(ex.Message);
    }
    finally
    {
    if(Fstream != null)
    {
    Fstream.Close();
    }
    context.Response.End();
    }
    }