c# - Why won't my code upload a file to the specified folder on the web server when using the fileupload control? -
good afternoon. have asp.net web forms application using c#. having difficulty getting code work properly. data uploaded sql server database, file isn't saved specified "data" folder. appreciated?
the id of fileupload control "fu_doc_upld". don't errors when using form, files aren't saving "data" folder (or other folder). here code behind i'm using:
protected void btn_frm_new_doc_save_close_click(object sender, eventargs e) { int = 0; string filename = fu_doc_upld.filename; if (fu_doc_upld.hasfile) { while (system.io.file.exists(server.mappath("~/data/") + filename)) { i++; filename = fu_doc_upld.filename + " (" + i.tostring() + ")"; fu_doc_upld.postedfile.saveas(server.mappath("~/data/") + filename); } } hdn_filename_txt.value = (server.mappath("~/data/") + filename); hdn_doc_uplod_dt_txt.value = datetime.now.tostring(); sqlconnection idrf_cnxn = new sqlconnection("data source=wdbsvcprd01\\svcdb;initial catalog=idrf;integrated security=true"); { sqlcommand new_doc_cmd = new sqlcommand("insert 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, 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(@vendor_id_fk)))", idrf_cnxn); new_doc_cmd.parameters.addwithvalue("@doc_title", doc_title_txt.text); new_doc_cmd.parameters.addwithvalue("@doc_type_list", doc_type_ddl.text); new_doc_cmd.parameters.addwithvalue("@doc_org_list", doc_org_ddl.text); new_doc_cmd.parameters.addwithvalue("@doc_dept_list", doc_dept_ddl.text); new_doc_cmd.parameters.addwithvalue("@doc_desc", doc_desc_txt.text); new_doc_cmd.parameters.addwithvalue("@prior_contract_cd", prior_contract_cd_txt.text); new_doc_cmd.parameters.addwithvalue("@legal_comp_contract_id", lgl_comp_cont_id_txt.text); new_doc_cmd.parameters.addwithvalue("@doc_upld_dt", hdn_doc_uplod_dt_txt.value); new_doc_cmd.parameters.addwithvalue("@doc_path", hdn_filename_txt.value); new_doc_cmd.parameters.addwithvalue("@vendor_id_fk", hdn_vendor_id_txt.value); idrf_cnxn.open(); new_doc_cmd.executenonquery(); idrf_cnxn.close(); if (ispostback) { response.redirect("~/default.aspx"); } } }
any appreciated.
thanks, j
you call saveas()
when file exists. could've found placing breakpoint , stepping through code.
move save out of loop:
string filename = fu_doc_upld.filename; while (system.io.file.exists(server.mappath("~/data/") + filename)) { i++; filename = fu_doc_upld.filename + " (" + i.tostring() + ")"; } fu_doc_upld.postedfile.saveas(server.mappath("~/data/") + filename);
note code remove extension if file exists. see c#: how make unique filename adding number? better implementation.
Comments
Post a Comment