2013-06-12 98 views
0

我有下面的C#代码:重命名上传的文件名C#

SelectQuery = string.Format("SELECT UserID from tblUsers WHERE Email='{0}'", Email); 
ds = DbQ.ExecuteQuery("SiteDB.mdb", SelectQuery); 
string UserID = ds.Tables[0].Rows[0]["UserID"].ToString(); 
if (Request.ContentLength != 0) 
{ 
    int Size = Request.Files[0].ContentLength/1024; 
    if (Size <= 512) 
    { 
     string LocalFile = Request.Files[0].FileName; 
     int LastIndex = LocalFile.LastIndexOf(@"\") + 1; 
     File = LocalFile.Substring(LastIndex, LocalFile.Length - LastIndex); 
    //  File = "ProfilePic-Id-" + UserID; 
     string Path = Server.MapPath("images/profiles/") + File; 
     Request.Files[0].SaveAs(Path); 

    } 
    else 
    { 
     Response.Write("The file is too big !"); 
    } 
} 
else 
{ 
    Response.Write("Unknown Error !"); 
} 

我想的是,上传的文件名重命名为“ProfilePic-ID-” +用户ID,我试图在发表评论,但它没不工作,我如何重命名上传的文件名?

希望得到帮助,谢谢!

+3

的SQL注入概率... –

+2

'但它没有工作...' - 什么** **没有发生? –

+3

我的电子邮件是''; DROP TABLE向tblUsers;' –

回答

2

怎么是这样的:

if (Size <= 512) 
{ 
    string path = string.Format("~/images/profiles/ProfilePic-Id-{0}.{1}", 
     UserID, System.IO.Path.GetExtension(Request.Files[0].FileName)); 
    Request.Files[0].SaveAs(Server.MapPath(path)); 

} 

看,当你说I get some unrecognized file ...,这是很明显的,因为你不设置它的延伸。文件字节可能就好了,它只是操作系统的一个未被识别的扩展名(即它甚至没有),所以你需要提供它。

+0

非常感谢yuo,现在我明白了! –

+0

@NaveTseva,没问题,我很高兴我可以帮助! –