The code below is supposed to rename a file with numbers if another file with the same name already exists. For example, test.docx, test(2).docx, etc... It works just fine on my localhost, but when I put it on the web server it changes the file name to test.docx(2). Any help would be greatly appreciated. Here's what I'm currently using:
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");
}
}
}
Any help would be greatly appreciated.
I'm using VS2013, framework is 4.5.1, this is an asp.net web forms application.
Thanks, J
So it should be somthing like this, if you read my prior comment:
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);
}
Here's the code...
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.