紧急求助
上传要求:上传任意类型的文件,用fileupload选择电脑里面的文件,点击一个上传button实现上传。上传的时候将文件移动到一个指定目录,然后将获取到的文件存放路径保存到数据库,方便下载的时候读取下载路径。
下载要求:下载任意类型的文件。根据从数据库读取出来的文件路径进行下载,可以下载到任意目录。类似于IE浏览器的目标另存为A。要求默认下载文件的默认名称为文件原始名称。
以上必须要做到在上传和下载的过程中不会对文件造成损害,我上传的是.txt的下载就必须是.txt的,上传是.exe,下载也必须是.exe

解决方案 »

  1.   

    上传
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = FileUpload1.FileName;//上传文件名字
            string size = FileUpload1.PostedFile.ContentLength.ToString();
            string type = FileUpload1.PostedFile.ContentType;
            string type2 = name.Substring(name.LastIndexOf(".") + 1);
            string ipath = Server.MapPath("upimg") + "\\" + name;
            string fpath = Server.MapPath("upfile") + "\\" + name;
            string path="F:\\aaa\\"+FileUpload1.FileName;
            string wpath = "upimg\\" + name;
           
                FileUpload1.SaveAs("F:\\aaa\\"+FileUpload1.FileName);
               // Image1.ImageUrl="F:\\aaa\\"+FileUpload1.FileName;
                Label1.Text = "你传图片的名字是" + name + "<br>文件大小为" + size + "<br>文件类型为" + type2 + "<br>文件路径为" + ipath;
            
            SqlConnection cn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");
            SqlCommand cmd = new SqlCommand("insert into Image(imageName,imagepath) values('" + name + "','" + path + "')", cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        protected void Button2_Click(object sender, EventArgs e)
        {    SqlConnection cn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");
        SqlCommand cmd = new SqlCommand("select imageName from Image where imageID='" + Convert.ToInt32(TextBox1.Text) + "'", cn);
        cn.Open();
        string a = cmd.ExecuteScalar().ToString();
        cn.Close();
        Image1.ImageUrl = "F:\\aaa\\" + a;
         }
    }
    下载[code=C#]#region 文件下载
           public bool DownLoadFile(string localPath, string hostURL, int byteCount, string userID, long cruuent)
           {
               
               bool result = true;
               
               
               string tmpURL = hostURL;
              
               byteCount = byteCount * 1024;
               hostURL = tmpURL + "&npos=" + cruuent.ToString();
               
               System.IO.FileStream fs;  
               fs = new FileStream(localPath, FileMode.OpenOrCreate);
               if (cruuent > 0)
               {
                   //偏移指针
                   fs.Seek(cruuent, System.IO.SeekOrigin.Current); 
               }
               System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(hostURL);
               if (cruuent > 0)
               {
                   request.AddRange(Convert.ToInt32(cruuent));    //设置Range值
               }           try
               {
                   //向服务器请求,获得服务器回应数据流
                   System.IO.Stream ns = request.GetResponse().GetResponseStream();               byte[] nbytes = new byte[byteCount];
                   int nReadSize = 0;
                   nReadSize = ns.Read(nbytes, 0, byteCount);
                  
                   while (nReadSize > 0)
                   {
                       fs.Write(nbytes, 0, nReadSize);
                       nReadSize = ns.Read(nbytes, 0, byteCount);
                      
                   }
                   fs.Close();
                   ns.Close();
               }
               catch(Exception ex)
               {
                   LOG.Error("下载" + localPath + "的时候失败!" + "原因是:" + ex.Message);
                   fs.Close();
                   result = false;
               }
               return result;     
           }
           #endregion [/code]
      

  2.   

    public bool DownLoadFile(string localPath, string hostURL, int byteCount, string userID, long cruuent)
    这几个参数分别是什么意思?  是不是我要下载文件的时候调用这个方法,然后传入那几个参数。请分别说明各个参数代表什么