![](/img/trans.png)
[英]The variable is not incremental after click the “Next” button using C# with asp.net web application
[英]How to add incremental numbers to the end of a filename when saving in asp.net web forms c# application?
如果另一個同名文件已經存在,下面的代碼應該用數字重命名文件。 例如,test.docx、test(2).docx 等......它在我的本地主機上工作得很好,但是當我把它放在 web 服務器上時,它會將文件名更改為 test.docx(2)。 任何幫助將不勝感激。 這是我目前正在使用的:
protected void btn_submit_new_doc_vsn_Click(object sender, EventArgs e)
{
int i = 0;
string filename = fu_new_doc_vsn.FileName;
if (fu_new_doc_vsn.HasFile)
{
while (System.IO.File.Exists(Server.MapPath("~/Data/") + filename))
{
i++;
filename = fu_new_doc_vsn.FileName + " (" + i.ToString() + ")";
}
fu_new_doc_vsn.PostedFile.SaveAs(Server.MapPath("~/Data/") + filename);
}
doc_path_text.Text = (Server.MapPath("~/Data/") + filename);
SqlConnection idrf_cnxn = new SqlConnection("Data Source=SRVER\\MYDB;Initial Catalog=idrf;Integrated Security=True");
{
SqlCommand new_doc_cmd = new SqlCommand("Insert Into tbl_doc(doc_title, doc_type_list, doc_org_list, doc_dept_list, doc_desc, prior_contract_cd, legal_comp_contract_id, doc_upld_dt, doc_path, crnt_doc_stat_list, vendor_id_fk) Values(LTRIM(RTRIM(@doc_title)), LTRIM(RTRIM(@doc_type_list)), LTRIM(RTRIM(@doc_org_list)), LTRIM(RTRIM(@doc_dept_list)), LTRIM(RTRIM(@doc_desc)), LTRIM(RTRIM(@prior_contract_cd)), LTRIM(RTRIM(@legal_comp_contract_id)), LTRIM(RTRIM(@doc_upld_dt)), LTRIM(RTRIM(@doc_path)), LTRIM(RTRIM(@crnt_doc_stat_list)), LTRIM(RTRIM(@vendor_id_fk)))", idrf_cnxn);
new_doc_cmd.Parameters.AddWithValue("@doc_title", doc_title_text.Text);
new_doc_cmd.Parameters.AddWithValue("@doc_type_list", doc_type_text.Text);
new_doc_cmd.Parameters.AddWithValue("@doc_org_list", doc_org_list_text.Text);
new_doc_cmd.Parameters.AddWithValue("@doc_dept_list", doc_dept_list_text.Text);
new_doc_cmd.Parameters.AddWithValue("@doc_desc", doc_desc_text.Text);
new_doc_cmd.Parameters.AddWithValue("@prior_contract_cd", prior_contract_cd_text.Text);
new_doc_cmd.Parameters.AddWithValue("@legal_comp_contract_id", legal_comp_contract_id_text.Text);
new_doc_cmd.Parameters.AddWithValue("@doc_upld_dt", doc_upld_dt_text.Text);
new_doc_cmd.Parameters.AddWithValue("@doc_path", doc_path_text.Text);
new_doc_cmd.Parameters.AddWithValue("@crnt_doc_stat_list", crnt_doc_stat_list_text.Text);
new_doc_cmd.Parameters.AddWithValue("@vendor_id_fk", vendor_id_fk_text.Text);
idrf_cnxn.Open();
new_doc_cmd.ExecuteNonQuery();
idrf_cnxn.Close();
if (IsPostBack)
{
Response.Redirect("~/Default.aspx");
}
}
}
任何幫助將不勝感激。
我使用的是 VS2013,框架是 4.5.1,這是一個 asp.net web 表單應用程序。
謝謝,J
所以它應該是這樣的,如果你閱讀我之前的評論:
int i = 0;
string filename = fu_new_doc_vsn.FileName;
if (fu_new_doc_vsn.HasFile)
{
while (System.IO.File.Exists(Server.MapPath("~/Data/") + filename))
{
i++;
filename = fu_new_doc_vsn.Name + " (" + i.ToString() + ")" + Path.GetFileNameWithoutExtension(fu_new_doc_vsn.FileName);
}
fu_new_doc_vsn.PostedFile.SaveAs(Server.MapPath("~/Data/") + filename);
}
這是代碼...
int i = 0;
string filename = fu_new_doc_vsn.FileName;
string fnnnoext = System.IO.Path.GetFileNameWithoutExtension(fu_new_doc_vsn.FileName);
string fnnextonly = System.IO.Path.GetExtension(fu_new_doc_vsn.FileName);
if (fu_new_doc_vsn.HasFile)
{
while (System.IO.File.Exists(Server.MapPath("~/Data/") + filename))
{
i++;
filename = (fnnnoext + "(" + i.ToString() + ")" + fnnextonly);
}
fu_new_doc_vsn.PostedFile.SaveAs(Server.MapPath("~/Data/") + filename);
}
hdn_doc_path_text.Value = (Server.MapPath("~/Data/") + filename);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.