重命名已经上传的文件 [英] Rename a file already uploaded

查看:69
本文介绍了重命名已经上传的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

我有一个Item Form Master,我在其中制作一个项目,在这里,我提供了在弹出窗口上上传该项目的图像并将其保存在与图像名称相同的文件夹中的功能.

现在,当我保存项目时,我想重命名上载的文件的名称,但我无法重命名该文件.

我试图保存的代码是:-

Dear Friends,

I have an Item Form Master where i am making an Item and here i have given a functionality of uploading an image for the item on a popup window and save it in a folder with the same name as image name.

Now when i am saving the item then i want to rename the name of the file uploaded but i am not able to rename the same.

The code with which i am trying to save the same is:-

if (!File.Exists(Server.MapPath("~/ItemImage/" + Convert.ToString(Request.QueryString["compid"]) + "/" + Convert.ToString(Request.QueryString["offID"]) + "/" + itemImgName)))
            {
                string newImgName = Convert.ToString(itemID);

                FileUpload file = new FileUpload();

                file.SaveAs(Server.MapPath("~/ItemImage/" + Convert.ToString(Request.QueryString["compid"]) + "/" + Convert.ToString(Request.QueryString["offID"]) + "/" + file.FileName.Replace(itemImgName + "." + fileExt, newImgName + "." + fileExt)));
            }



上面的代码执行成功,但是图像名称没有按照我的要求重命名.

请让我知道我要去哪里了,或者还有其他选择可以这样做.



The above code is executing successfully but the image name doesn''t get renamed as per my requirement.

Please let me know where i am going wrong or there is some other option of doing the same.

推荐答案

请尝试保存新文件并删除旧文件. br/>
try by saving new file and deleting the old file..

if (!File.Exists(Server.MapPath("~/ItemImage/" + Convert.ToString(Request.QueryString["compid"]) + "/" + Convert.ToString(Request.QueryString["offID"]) + "/" + itemImgName)))
            {
                string newImgName = Convert.ToString(itemID);
 
                FileUpload file = new FileUpload();
 
                file.SaveAs(Server.MapPath("~/ItemImage/" + Convert.ToString(Request.QueryString["compid"]) + "/" + Convert.ToString(Request.QueryString["offID"]) + "/" + newImgName + "." + fileExt)));

                FileInfo MyFile = new FileInfo(Server.MapPath("~/ItemImage/" + Convert.ToString(Request.QueryString["compid"]) + "/" + Convert.ToString(Request.QueryString["offID"]) + "/" + itemImgName + "." + fileExt));
          
MyFile.Delete();
            }


这篇关于重命名已经上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆