2013-08-07 13 views
8

保存的文件名昌我有文件上傳,通過它我瀏覽文件 和strore的字符串變量瀏覽文件的名稱。 現在我想與存儲在另一個字符串變量重命名文件時,它是通過文件上傳控制上傳和數據庫

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此變量包含與我想用它來重命名文件上傳,通過上傳的文件名的另一個名字重新命名這個uplaod文件名。 通過文件名存儲在文件上傳strfilename控制上傳。 所以指定的文件夾 在保存此文件之前,我想將它與strRoleValue值重新命名。 我如何能實現this..plz幫助

回答

10

您可以檢查出以下鏈接

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> 
+0

它的作品!謝謝! – shweta