回 帖 发 新 帖 刷新版面

主题:[讨论]使用upload控件怎么使上传文件重新命名?

使用upload控件怎么使上传文件重新命名?
protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string filetype = FileUpload1.PostedFile.ContentType;
            if (filetype == "text/plain")
            {
                try
                {
                    string name = FileUpload1.PostedFile.FileName; // 客户端文件路径
                    FileInfo file = new FileInfo(name);
                    string filename = file.Name;
                    string rndday = System.DateTime.Today.Year.ToString() + System.DateTime.Today.Month.ToString()+System.DateTime.Today.Day.ToString();
                    string rndnow = System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
                    string webpath = Server.MapPath("Uploads/")+rndday+rndnow+filename;
                    FileUpload1.SaveAs(webpath);
                    Label1.Text = "File name: " +
                    FileUpload1.PostedFile.FileName + "<br>" +
                    FileUpload1.PostedFile.ContentLength + " kb<br>" + "Content type: " +
                    FileUpload1.PostedFile.InputStream.Length.ToString();
                }
                catch (Exception ex)
                {
                    Label1.Text = "ERROR: " + ex.Message.ToString();
                }

            }
            else
            {
                Response.Write("文件类型非法!");
            }

        }

回复列表 (共4个回复)

沙发

你不是已经用了SaveAs吗,存的时候改个名字即可

板凳

没有很仔细的看你的程序,我给你一个我自己写的程序段:
private void bnbvalidate_Click(object sender, System.EventArgs e)
        {
            DateTime now = DateTime.Now;  //取现在时间到DataTime类的对象now中
            string strBaseLocation = "http://localhost/SFC/Photo/";  //这是文件将上传到的服务器的目录
            if (uploadfile1.PostedFile.ContentLength != 0) //判断选取对话框选取的文件长度是否为0
          {
                int i;
                int x=0;
                for(i = 0 ;i < uploadfile1.PostedFile.FileName.Length; i++)
                {
                    string stri=uploadfile1.PostedFile.FileName.Substring(i,1);
                    if(stri==".")
                    {
                        x=i;
                    }
                }
                string str = uploadfile1.PostedFile.FileName.Substring(x,uploadfile1.PostedFile.FileName.Length - x);
                string FileName = now.DayOfYear.ToString() + uploadfile1.PostedFile.ContentLength.ToString() + str;
            
                string PNum = FileName;
                string intro = this.txtintro.Text.ToString();
                string address = strBaseLocation + FileName;
                string CCode = Session["ClassNum"].ToString();
                string UCode = Session["UNum"].ToString();
                DateTime stime = System.DateTime.Now;

                if(UCode.Equals(""))
                {
                    this.Label1.Text = "你还没登陆,无法上传图片!";
                }    
                else
                {
                    try
                    {
                        uploadfile1.PostedFile.SaveAs(Server.MapPath("\\SFC\\Photo\\") + FileName); //执行上传,并自动根据日期和文件大小不同为文件命名,确保不重复                
                        ClassComponent.DoubleClass obj=new ClassComponent.DoubleClass();
                        string message=obj.PhotoSave(PNum,intro,address,CCode,UCode,stime).ToString();
                        this.Label1.Text = message;
                    }
                    catch(Exception es)
                    {
                        string message=es.Message;
                        this.Label1.Text = message;
                    }
                }
            }
        }

3 楼

for(i = 0 ;i < uploadfile1.PostedFile.FileName.Length; i++)
                {
                    string stri=uploadfile1.PostedFile.FileName.Substring(i,1);
                    if(stri==".")
                    {
                        x=i;
                    }
                }
                string str = uploadfile1.PostedFile.FileName.Substring(x,uploadfile1.PostedFile.FileName.Length - x);

请问这段代码的意思?谢谢!

4 楼

获取上传文件的后缀,可以用在后面判断上传文件的格式.

我来回复

您尚未登录,请登录后再回复。点此登录或注册