正如我们平时在网上下载一些有用的视频课件.压缩文件.word文档等.
这个功能如何用c#实现啊?有没有特别的函数?望赐教.谢谢

解决方案 »

  1.   

    给你贴出我的源代码吧,其实这个有两种实现方式:一种是不写在数据库的请看:看看孟子的方法吧   
      http://dotnet.aspx.cc/ShowDetail.aspx?id=58EA3515-36F2-4FD9-AC89-EAF49F59816C ;
     另一种写到数据库的方法是我自己写的请参考吧:上传和下载都有:上传为:
    string FileName="";
    int pos=0;
    if (this.filename.PostedFile!=null)
    {
    FileName=this.filename.PostedFile.FileName.ToString();
    pos=FileName.LastIndexOf("\\");
    FileName=FileName.Substring(pos+1,FileName.Length-pos-1);
    Byte[] photo=new byte[this.filename.PostedFile.ContentLength];
    this.filename.PostedFile.InputStream.Read (photo,0,this.filename.PostedFile.ContentLength);
    cEmsAttachmentsEntity cAe = new cEmsAttachmentsEntity();
    cAe.OperationId=Request["OperationId"].ToString();
    cAe.OpSeqNum=Request["OperationId"].ToString();
    cAe.Name =this.AttName.Value.ToString().Trim();
    cAe.DocExplain =this.AttExplain.Value.ToString().Trim();
    cAe.Property ="0";
    cAe.FileName=FileName;
    cAe.Entity=photo;
    cEmsAttachments.AddAttachments(cAe);

    Response.Redirect("AddDocument.aspx?OperationId="+Request["OperationId"],false);  
    下载为:byte[] photo=new byte[0];
    string OperationId=this.Request["OperationId"].ToString();
    string FileName="";
    SqlConnection Conn = new SqlConnection(cEmsDataConnString.EMonitor);
    SqlCommand logoCMD = new SqlCommand("select FileName,Entity from OP_Attachments where OperationId="+OperationId, Conn);
    Conn.Open();
    SqlDataReader myReader = logoCMD.ExecuteReader(CommandBehavior.SequentialAccess); int bufferSize = 100;                   // Size of the BLOB buffer.
    byte[] outbyte = new byte[bufferSize];  // The BLOB byte[] buffer to be filled by GetBytes.
    long retval;                            // The bytes returned from GetBytes.
    long startIndex = 0;                    // The starting position in the BLOB output. //string pub_id = "";                     // The publisher id to use in the file name.

    this.Response.Clear();
    this.Response.ContentType="application/x-msdownload";
    this.Response.ContentEncoding = Encoding.Default; 
    while (myReader.Read())
    {
    // Get the publisher id, which must occur before getting the logo.
    FileName = myReader.GetString(0);  
                    
    //this.Response.AddHeader("Content-disposition","attachment;StencilName="+ Encoding.Default.GetString(Encoding.Unicode.GetBytes(StencilName)));
    this.Response.AddHeader("Content-disposition","attachment;filename="+ HttpUtility.UrlEncode(FileName));
    // Read the bytes into outbyte[] and retain the number of bytes returned.
    retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
    this.Response.BinaryWrite(outbyte); // Continue reading and writing while there are bytes beyond the size of the buffer.
    while (retval == bufferSize)
    {
    // Reposition the start index to the end of the last buffe0r and fill the buffer.
    startIndex += bufferSize;
    retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
    this.Response.BinaryWrite(outbyte);
    }
    }
    this.Response.End() ; // Close the reader and the connection.
    myReader.Close();
    Conn.Close();
      

  2.   

    看你是什么协议的,
    如果是http或者ftp的,你可以用webclient或者用socket去写webclient的话,你可以参看msdn的例子;而用socket的话,你可以去网上找找例子,例如:
    http://www.codeproject.com/csharp/ftp.asp