8

我有文件上传器,通过它我浏览文件并在字符串变量中存储浏览文件的名称。现在我想用存储在另一个字符串变量中的另一个名称重命名这个上传文件名

string strRoleValue = ddlrole.SelectedValue;

 string strfilename = FileUpload1.FileName;
 string existpath = Server.MapPath("~\\JD\\");
 DirectoryInfo ObjSearchFile = new DirectoryInfo(existpath);
  string saveLocation = existpath + strfilename;
FileUpload1.SaveAs(saveLocation);

strRoleValue 这个变量包含我想用来重命名通过 fileupload 上传的文件的名称。通过fileupload控件上传的文件名存储在strfilename. 所以在将此文件保存到指定文件夹之前,我想用 strRoleValue 值重命名它。我怎样才能做到这一点..plz帮助

4

2 回答 2

12

您可以查看以下链接

http://asp-net-example.blogspot.in/2009/01/aspnet-fileupload-example-how-to-rename.html

<%@ Page Language="C#" %>  
<%@ Import Namespace="System.IO" %>  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e) {  
        string uploadFolder = Request.PhysicalApplicationPath + "UploadFile\\";  
        if (FileUpload1.HasFile)  
        {  
            string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);  
            FileUpload1.SaveAs(uploadFolder + "Test"+ extension);  
            Label1.Text = "File uploaded successfully as: " + "Test"+ extension;  
        }  
        else  
        {  
            Label1.Text = "First select a file.";  
        }  
    }  
</script>  

<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>asp.net FileUpload example: how to rename file when upload (change file name when upload)</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:Green">asp.net FileUpload example: File Rename</h2>  
        <asp:Label   
             ID="Label1"   
             runat="server"   
             Font-Size="Large"  
             ForeColor="OrangeRed"  
             >  
        </asp:Label>  
        <br /><br />  
        <asp:FileUpload   
             ID="FileUpload1"   
             runat="server"   
             BackColor="DeepPink"   
             ForeColor="AliceBlue"   
             />  
        <asp:Button   
             ID="Button1"   
             runat="server"   
             Font-Bold="true"   
             ForeColor="DeepPink"   
             OnClick="Button1_Click"  
             Text="Upload It"  
             />     
    </div>  
    </form>  
</body>  
</html>  
于 2013-08-07T06:33:54.057 回答
0
<input name="fileUploadedButtonName" type="file" />
public ActionResult Register(HttpPostedFileBase fileUploadedButtonName){

 string newFileName= "";

 if (fileUploadedButtonName != null)
   {
     string path = HttpContext.Server.MapPath(@"~/YourFolderName");

     bool exists = System.IO.Directory.Exists(path);

     if (!exists)
        System.IO.Directory.CreateDirectory(path);

     string extension = Path.GetExtension(fileUploadedButtonName.FileName);
     newFileName= Guid.NewGuid() + extension;
     string filePath = Path.Combine(path, newFileName);
     fileUploadedButtonName.SaveAs(filePath);

   }

}
于 2019-11-28T18:51:55.443 回答