求 fileupload 上传文件的代码 ,不只上传图片,还可以上传其它文件 

解决方案 »

  1.   

    http://blog.csdn.net/aifox/archive/2007/01/11/1480337.aspx去这里看看。上面讲得很详细的。还有代码的
      

  2.   

     filename = FileUpload1.FileName;
                if (filename.ToString() != "")
                {
                    string name = filename.Substring(filename.LastIndexOf('.'), filename.Length - filename.LastIndexOf('.'));//获取文件后缀名
                    string fileurl = Server.MapPath("/mp3/");
                    if (name.Equals(".mp3") || name.Equals(".wma"))
                    {
                        try
                        {
                            string fn = DateTime.Now.ToString("yyyyMMddhhmmss");
                            fn = fn + name;                        FileUpload1.SaveAs(fileurl + fn);
                                                }
                        catch (Exception ex)
                        { Response.Write("<script> arct('上传失败请重新上传')</script>"); }
                    }
      

  3.   

    1 楼那个好像是java的 
      

  4.   

    使用FileUpload控件。
    if (upImage.HasFile)
                {
     string filePath = "~/upload/" + upImage.FileName;
                        upImage.SaveAs(MapPath(filePath));
    }
      

  5.   

    跪求ASP。NET和C#的师付,QQ15035195
      

  6.   

    去看一下stream的资料,你就会了
      

  7.   

      /// 
            /// 在根目录下创建文件夹 
            /// 
            /// 要创建的文件路径 
            public void CreateFolder(string FolderPathName)
            {
                if (FolderPathName.Trim().Length > 0)
                {
                    try
                    {
                        string CreatePath = System.Web.HttpContext.Current.Server.MapPath                    ("work/" + FolderPathName).ToString();
                        if (!Directory.Exists(CreatePath))
                        {
                            Directory.CreateDirectory(CreatePath);
                        }
                    }
                    catch
                    {
                        throw;
                    }
                }
            }        /**/
            /// 
            /// 删除一个文件夹下面的字文件夹和文件 
            /// 
            /// 
            public void DeleteChildFolder(string FolderPathName)
            {
                if (FolderPathName.Trim().Length > 0)
                {
                    try
                    {
                        string CreatePath = System.Web.HttpContext.Current.Server.MapPath                    (FolderPathName).ToString();
                        if (Directory.Exists(CreatePath))
                        {
                            Directory.Delete(CreatePath, true);
                        }
                    }
                    catch
                    {
                        throw;
                    }
                }
            }        /**/
            /// 
            /// 删除一个文件 
            /// 
            /// 
            public void DeleteFile(string FilePathName)
            {
                try
                {
                    FileInfo DeleFile = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(FilePathName).ToString());
                    DeleFile.Delete();
                }
                catch
                {
                }
            }
            public void CreateFile(string FilePathName) 
            { 
                try 
                { 
                    //创建文件夹 
                    string[] strPath= FilePathName.Split('/'); 
                    CreateFolder(FilePathName.Replace("/" + strPath[strPath.Length-1].ToString(),"")); //创建文件夹
                    FileInfo CreateFile =new FileInfo(System.Web.HttpContext.Current.Server.MapPath(FilePathName).ToString()); //创建文件 
                    if(!CreateFile.Exists) 
                    { 
                    FileStream FS=CreateFile.Create(); 
                    FS.Close(); 
                    } 
                } 
                catch 
                {  } 
            }
            /**/
            /// 
            /// 删除整个文件夹及其字文件夹和文件 
            /// 
            /// 
            public void DeleParentFolder(string FolderPathName)
            {
                try
                {
                    DirectoryInfo DelFolder = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("work/" + FolderPathName).ToString());
                    if (DelFolder.Exists)
                    {
                        DelFolder.Delete();
                    }
                }
                catch
                {
                }
            }
            /**/
            /// 
            /// 在文件里追加内容 
            /// 
            /// 
            public void ReWriteReadinnerText(string FilePathName, string WriteWord)
            {
                try
                {
                    //建立文件夹和文件 
                    //CreateFolder(FilePathName); 
                    CreateFile(FilePathName);
                    //得到原来文件的内容 
                    FileStream FileRead = new FileStream(System.Web.HttpContext.Current.Server.MapPath                (FilePathName).ToString(), FileMode.Open, FileAccess.ReadWrite);
                    StreamReader FileReadWord = new StreamReader(FileRead, System.Text.Encoding.Default);
                    string OldString = FileReadWord.ReadToEnd().ToString();
                    OldString = OldString + WriteWord;
                    //把新的内容重新写入 
                    StreamWriter FileWrite = new StreamWriter(FileRead, System.Text.Encoding.Default);
                    FileWrite.Write(WriteWord);
                    //关闭 
                    FileWrite.Close();
                    FileReadWord.Close();
                    FileRead.Close();
                }
                catch
                {
                    // throw; 
                }
            }        /**/
            /// 
            /// 在文件里追加内容 
            /// 
            /// 
            public string ReaderFileData(string FilePathName)
            {
                try
                {                FileStream FileRead = new FileStream(System.Web.HttpContext.Current.Server.MapPath                (FilePathName).ToString(), FileMode.Open, FileAccess.Read);
                    StreamReader FileReadWord = new StreamReader(FileRead, System.Text.Encoding.Default);
                    string TxtString = FileReadWord.ReadToEnd().ToString();
                    //关闭 
                    FileReadWord.Close();
                    FileRead.Close();
                    return TxtString;
                }
                catch
                {
                    throw;
                }
            }
            /**/
            /// 
            /// 读取文件夹的文件 
            /// 
            /// 
            /// 
            public DirectoryInfo checkValidSessionPath(string FilePathName)
            {
                try
                {
                    DirectoryInfo MainDir = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath                (FilePathName));
                    return MainDir;            }
                catch
                {
                    throw;
                }
            } 
    这是所以有关文件操作的代码,希望能看懂
      

  8.   

    protected void btnupload_Click(object sender, EventArgs e)
        {
            try
            {
                Upload(FileUpload9);
                Upload(FileUpload8);
                Upload(FileUpload7);
                Upload(FileUpload6);
                Upload(FileUpload5);
                Upload(FileUpload4);
                Upload(FileUpload3);
                Upload(FileUpload2);
                Upload(FileUpload10);
                Upload(FileUpload1);            txtmsg.Text = "完成文件上传,如下:<BR>" + ViewState["Upload"].ToString();
                ViewState["Upload"] = null;
            }
            catch (Exception ex) 
            {
                txtmsg.Text = ex.Message;
                //txtmsg.Text = "指定文件";
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("上传信息是:<hr color=red>");
        }
        private void Upload(FileUpload myfileupload) 
        {
            string path = HttpContext.Current.Request.MapPath("~/img/");
            if (myfileupload.HasFile)
            {
                try
                {
                    DirectoryInfo dfile = new DirectoryInfo(path);
                    FileInfo[] file = dfile.GetFiles(myfileupload.FileName);
                    if (file.Length == 1)
                    {
                        txtmsg.Text = "有同名文件夹";
                    }
                    else
                    {
                        myfileupload.SaveAs(path + myfileupload.FileName);
                        ViewState["Upload"] += myfileupload.PostedFile.FileName + "</br>";
                    }
                }
                catch (Exception ex)
                {
                    txtmsg.Text += ex.Message;
                }
            }
            else 
            {
                if(hasfile().ToString()!=null)
                {
                    txtmsg.Text = "必须指定一个文件!" + hasfile().ToString();
                    //txtmsg.Text = hasfile().ToString();
                }
            }
        }
        private bool hasfile() 
        {
            bool status = false;
            status = FileUpload1.HasFile && FileUpload10.HasFile && FileUpload2.HasFile &&
                FileUpload3.HasFile && FileUpload4.HasFile && FileUpload5.HasFile &&
                FileUpload6.HasFile && FileUpload7.HasFile && FileUpload8.HasFile &&
                FileUpload9.HasFile;
            return status;
        }
    这个是批量上传的,去年写的,可能有点老旧···
      

  9.   

    protected void btnUpload_Click(object sender, EventArgs e) 
        {  
            if (fileUpload.HasFile) 
            { 
                string savePath = Server.MapPath("~/upload/"); 
                if (!System.IO.Directory.Exists(savePath)) 
                { 
                    System.IO.Directory.CreateDirectory(savePath); 
                } 
                savePath = savePath + "\\" + fileUpload.FileName; 
                fileUpload.SaveAs(savePath);//保存文件 
                
            } 
        } 
      

  10.   

     protected void btnShangChuan_Click(object sender, EventArgs e)
        {
            string path = Server.MapPath("~/File/");
            //判断上传文件大小
            /*限制文件大小在配置文件中httpRuntime 下的maxRequestLength属性,单位字节;
             */
            if (fiLoad.PostedFile.ContentLength > 文件长度)
            {
                labname.Text = "上传文件大小不能超过文件大小";
                return;
            }
             //上传控件中是否有该文件
            else if (fiLoad.HasFile)
            {
                string filename = fiLoad.FileName;
                //判断该文件名是否存在
                DirectoryInfo di = new DirectoryInfo(path);
                path += filename;
                if (di.Exists)
                {
                    FileInfo[] diinfo = di.GetFiles();
                    foreach (FileInfo item in diinfo)
                    {
                        if (item.Name == filename)
                        {
             //myUpload()为页面函数,函数中用confirm(),然后再在函数中调用后台DataBD()方法
                            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "a", "<script >myUpload()</script>");
                            return;
                        }
                    }
                    fiLoad.SaveAs(path);
                    labname.Text = "你成功上传文件 “" + filename + "”";
                    ShowDataBD();
                }
                else
                {
                    fiLoad.SaveAs(path);
                    labname.Text = "你成功上传文件 “" + filename + "”";
                     ShowDataBD();
                }
            }
            else
            {
                labname.Text = "请选择要上传的文件";
            }
        }
     public string DataBD()
        {
            string path = Server.MapPath("~/File/");
            if (fiLoad.HasFile)
            {
                string filename = fiLoad.FileName;
                path += filename;
                fiLoad.SaveAs(path);
                return "你成功替换了文件“" + filename + "”";
            }
            return null;
        }