前台写了个FileUpload和Button,后台怎么写?

解决方案 »

  1.   

    http://www.cnblogs.com/qiantuwuliang/archive/2009/08/21/1551200.html
      

  2.   


     protected void btnOK_Click(object sender, EventArgs e)
        {
            //上传列表图片
            try
            {
                string datedir = DateTime.Now.ToString("yyyyMMdd");
                string name = Maticsoft.Utility.Tool.Upload(myFileUpload, new string[] { ".jpg", ".png", ".gif" }, 3, Server.MapPath("../download/"));
                
            }
            catch (Exception ex)
            {
                //错误        }
        }
      

  3.   


    傻了。竟然忘记贴函数了。SORRY    public static string Upload(FileUpload myFileUpload, string[] allowExtensions, int maxLength, string savePath)
            {
                // 文件格式是否允许上传
                bool fileAllow = false;            //检查是否有文件案
                if (myFileUpload.HasFile)
                {
                    // 检查文件大小, ContentLength获取的是字节,转成M的时候要除以2次1024
                    if (myFileUpload.PostedFile.ContentLength / 1024 / 1024 >= maxLength)
                    {
                        throw new Exception("只能上传小于" + maxLength + "M的文件!");
                    }                //取得上传文件之扩展文件名,并转换成小写字母
                    string fileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();
                    string tmp = "";   // 存储允许上传的文件后缀名
                    //检查扩展文件名是否符合限定类型
                    for (int i = 0; i < allowExtensions.Length; i++)
                    {
                        tmp += i == allowExtensions.Length - 1 ? allowExtensions[i] : allowExtensions[i] + ",";
                        if (fileExtension == allowExtensions[i])
                        {
                            fileAllow = true;
                        }
                    }                if (fileAllow)
                    {
                        try
                        {
                            string datedir = DateTime.Now.ToString("yyyyMMdd");
                            if (!Directory.Exists(savePath + datedir))
                            {
                                Directory.CreateDirectory(savePath + datedir);
                            }
                            string saveName = Guid.NewGuid() + fileExtension;
                            string path = savePath + datedir + "/" + saveName;
                            //存储文件到文件夹
                            myFileUpload.SaveAs(path);
                            return datedir + "/" + saveName;
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(ex.Message);
                        }
                    }
                    else
                    {
                        throw new Exception("文件格式不符,可以上传的文件格式为:" + tmp);
                    }
                }
                else
                {
                    throw new Exception("请选择要上传的文件!");
                }
            }
      

  4.   

     protected void UploadButton_Click(object sender, EventArgs e) 
        { 
            string uploadName = InputFile.Value;//获取待上传图片的完整路径,包括文件名 
            //string uploadName = InputFile.PostedFile.FileName; 
            string pictureName = "";//上传后的图片名,以当前时间为文件名,确保文件名没有重复 
            if (InputFile.Value != "") 
            { 
                int idx = uploadName.LastIndexOf("."); 
                string suffix = uploadName.Substring(idx);//获得上传的图片的后缀名 
                pictureName = DateTime.Now.Ticks.ToString() + suffix; 
            } 
            try 
            { 
                if (uploadName != "") 
                { 
                    string path = Server.MapPath("~/images/"); 
                    InputFile.PostedFile.SaveAs(path + pictureName); 
                } 
            } 
            catch (Exception ex) 
            { 
                Response.Write(ex); 
            } 
        }
      

  5.   

     protected void UploadButton_Click(object sender, EventArgs e) 
        { 
            string uploadName = InputFile.Value;//获取待上传图片的完整路径,包括文件名 
            //string uploadName = InputFile.PostedFile.FileName; 
            string pictureName = "";//上传后的图片名,以当前时间为文件名,确保文件名没有重复 
            if (InputFile.Value != "") 
            { 
                int idx = uploadName.LastIndexOf("."); 
                string suffix = uploadName.Substring(idx);//获得上传的图片的后缀名 
                pictureName = DateTime.Now.Ticks.ToString() + suffix; 
            } 
            try 
            { 
                if (uploadName != "") 
                { 
                    string path = Server.MapPath("~/images/"); 
                    InputFile.PostedFile.SaveAs(path + pictureName); 
                } 
            } 
            catch (Exception ex) 
            { 
                Response.Write(ex); 
            } 
        }