asp.net – 当通过文件上传控件上传文件时,在c#.net中重命名文件,并将文件的chang名称保存在数据库中

前端之家收集整理的这篇文章主要介绍了asp.net – 当通过文件上传控件上传文件时,在c#.net中重命名文件,并将文件的chang名称保存在数据库中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有文件上传器通过它我浏览文件
并在字符串变量中输出浏览文件名称.
现在我想用另一个存储在另一个字符串变量中的名称重命名这个uplaod文件
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上传文件名称.
通过存储在strfilename中的fileupload控件上传文件名.
所以在将此文件保存到指定文件夹之前
我想用strRoleValue值重命名它.
我怎样才能实现这个… plz的帮助

解决方法

您可以查看以下链接

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>

猜你在找的asp.Net相关文章