【问题标题】:Rename the uploaded file name C#重命名上传的文件名 C#
【发布时间】:2013-06-13 01:11:37
【问题描述】:

我有以下 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,我在评论中试过了,但是没有成功,如何重命名上传的文件名?

希望得到帮助,谢谢!

【问题讨论】:

  • SQL注入概率...
  • but it didn't work ... -发生了什么
  • 我的邮箱是'; DROP TABLE tblUsers;
  • 图像保存不好,我得到了一些无法识别的文件,图像根本没有保存...
  • @BradM 我的是' OR '' = '

标签: c# .net file-upload


【解决方案1】:

这样的事情怎么样:

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 ... 时,这很明显,因为您没有在其上设置扩展名。文件字节可能很好,它只是操作系统无法识别的扩展名(即它甚至没有),所以你需要提供它。

【讨论】:

  • 非常感谢,现在我明白了!
  • @NaveTseva,没问题,我很高兴能为您提供帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
  • 1970-01-01
  • 1970-01-01
  • 2012-06-24
  • 2017-03-19
相关资源
最近更新 更多