简体   繁体   中英

Why won't my code upload a file to the specified folder on the web server when using the fileupload control?

Good afternoon. I have an asp.net web forms application using c#. I am having some difficulty getting my code to work properly. The data is uploaded successfully to the sql server database, but the file isn't saved to the specified "Data" folder. any help would be greatly appreciated?

The ID of the fileupload control is "fu_doc_upld". I don't get any errors when using the form, the files just aren't saving to the "Data" folder (or any other folder). Here is the code behind that I'm using:

protected void btn_frm_new_doc_save_close_Click(object sender, EventArgs e)
    {
        int i = 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 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, 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 help would be greatly appreciated.

Thanks, J

You only call SaveAs() when the file exists. You could've found this by placing a breakpoint and stepping through your code.

Move the save out of the 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 that this code will remove the extension if a file already exists. See C#: How would you make a unique filename by adding a number? for a better implementation.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM