重命名图片名称 [英] Rename the image name
本文介绍了重命名图片名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为theme.jpg的图像.现在,每当我上传新图像时,都应使用名称theme.jpg进行保存,而以前的theme.jpg应该重命名为theme1.jpg.以同样的方式,我必须将图像重命名为theme2.jpg,theme3.jpg ..............新上传的图像应始终保存为theme.jpg
I have a image called theme.jpg. Now whenever i upload a new image it should be saved with the name theme.jpg and the previous theme.jpg should be renamed as theme1.jpg. In the same way i have to rename the images as theme2.jpg,theme3.jpg..............The new uploaded image should be saved as theme.jpg all the time
推荐答案
.cs文件
.cs file
{
FileUpload AdImageFile = (FileUpload)(e.Item.Cells[1].Controls[1]);
if (File.Exists(Server.MapPath("Ad_Picture/" + dgadvetise.DataKeys[e.Item.ItemIndex] + ".jpg")))
File.Delete(Server.MapPath("Ad_Picture/" + dgadvetise.DataKeys[e.Item.ItemIndex] + ".jpg"));
AdImageFile.SaveAs(Server.MapPath("Ad_Picture/" + dgadvetise.DataKeys[e.Item.ItemIndex] + ".jpg"));
cls.UpdateData("AdDetails", new string[] { "AdName", "AdLink", "AdImagePath" }, new string[] { AdName.Text.ToString(), AdLink.Text.ToString(), dgadvetise.DataKeys[e.Item.ItemIndex] + ".jpg" }, "Where AdID=" + dgadvetise.DataKeys[e.Item.ItemIndex], "Image Details Updated", this.Page);
}
public void UpdateData(string tblName, string[] colName, string[] colValue, string searchCodn, string msg, Page form)
{
try
{
if (colName.Length != colValue.Length)
{
MsgBox("Column Name not equal Column Value", form);
return;
}
string sqlQuery = "select * from " + tblName + " " + searchCodn;
adp = new SqlDataAdapter(sqlQuery, conn);
tbl = new DataTable();
adp.Fill(tbl);
if (tbl.Rows.Count == 0)
{
return;
}
for (int i = 0; i < colName.Length; i++)
{
if ((tbl.Columns[colName[i]].DataType.ToString() == "System.Int16" | tbl.Columns[colName[i]].DataType.ToString() == "System.Int32" | tbl.Columns[colName[i]].DataType.ToString() == "System.Int64" | tbl.Columns[colName[i]].DataType.ToString() == "System.Decimal"))
{
if (colValue[i] != null)
tbl.Rows[0][colName[i]] = Convert.ToInt64(colValue[i]);
}
else
{
if (colValue[i] != null)
tbl.Rows[0][colName[i]] = colValue[i];
}
}
cmdBld = new SqlCommandBuilder(adp);
adp.Update(tbl);
tbl.Dispose();
adp.Dispose();
cmdBld.Dispose();
if (msg != null)
MsgBox(msg, form);
}
catch (Exception ex)
{
MsgBox("Update Data Error: " + ex.Message, form);
}
}
.cs.aspx文件
.cs.aspx file
<asp:DataGrid ID="dgadvetise" runat="server" AutoGenerateColumns="False"
PageSize="5" Width="750px" AllowPaging="True" AllowSorting="True" DataKeyField="AdID">
<Columns>
<asp:BoundColumn DataField="AdID" HeaderText="Image ID" Visible="False" />
<asp:TemplateColumn HeaderText="Image">
<ItemTemplate>
<ul class="hoverbox">
<li>
<a href="#"><img id="imgSmall" src='Ad_Picture/<%# DataBinder.Eval(Container, "DataItem.AdImagePath") %>' alt="Gallery Image" width="100" height="100" /></a>
</li>
</ul>
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="flUploadGrid" runat="server" ForeColor="#2A2F0F"></asp:FileUpload>
</EditItemTemplate>
<HeaderStyle Width="110px" />
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
使用此代码来重命名文件
Use this Code to rename the file
Imports System.IO
File.Move(@"C:\Dir1\SomeFile.txt", @"C:\Dir1\RenamedFileName.txt")
尝试使用以下代码使用theme.jpg保存文件:
Try this code save u r file with theme.jpg:
filename = Server.MapPath("~/Uploads/" + theme.jpg);
FileUpload1.SaveAs(filename);
然后读取服务器文件夹中的所有文件,并使用以下代码重命名它们:
then read for all the files in the server folder and rename them using the following code:
File.Copy(oldFileName, NewFileName);
File.Delete(oldFileName);
这篇关于重命名图片名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文