Unable to upload documents to server using file upload control in asp.net c# web forms application

Discussion in 'Site Programming, Development and Design' started by Jerald, Mar 10, 2016.

Tags:
  1. I have an asp.net c# web forms application that uses a file upload control to load documents to a specific folder. The location to upload the file is E:\web\me\Assignments\assignment_docs\.

    The error code is:

    The following is the code behind:

    Code:
    protected void btn_fuc_assnmt_doc_Click(object sender, EventArgs e)
                {
                    int i = 0;
                    string filename = fuc_assnmt_doc.FileName;
                    if (fuc_assnmt_doc.HasFile)
                    {
                        while (System.IO.File.Exists(Server.MapPath("~/Assignments/assignment_docs/") + filename))
                        {
                            i++;
                            filename = fuc_assnmt_doc.FileName + " (" + i.ToString() + ")";
                        }
                        fuc_assnmt_doc.PostedFile.SaveAs(Server.MapPath("~/Assignments/assignment_docs/") + filename);
                    }
       
                    SqlConnection azunl_cnxn = new SqlConnection("Data Source=tcp:s09.winhost.com;Initial Catalog=XXXX;User ID=XXXX;Password=XXXX;Integrated Security=False;");
                    {
                        SqlCommand new_doc_cmd = new SqlCommand("Insert Into tbl_assnmt_doc(assnmt_doc_title, assnmt_doc_type, assnmt_doc_upld_by ,assnmt_doc_upld_dt, assnmt_doc_path, assnmt_doc_desc, assnmt_id_fk) Values(LTRIM(RTRIM(@assnmt_doc_title)), LTRIM(RTRIM(@assnmt_doc_type)), LTRIM(RTRIM(@assnmt_doc_upld_by)), LTRIM(RTRIM(@assnmt_doc_upld_dt)), LTRIM(RTRIM(@assnmt_doc_path)), LTRIM(RTRIM(@assnmt_doc_desc)), LTRIM(RTRIM(@assnmt_id_fk)))", azunl_cnxn);
       
                        new_doc_cmd.Parameters.AddWithValue("@assnmt_doc_title", assnmt_doc_title_txt.Text);
                        new_doc_cmd.Parameters.AddWithValue("@assnmt_doc_type", assnmt_doc_type_ddl.Text);
                        new_doc_cmd.Parameters.AddWithValue("@assnmt_doc_upld_by", System.Web.HttpContext.Current.User.Identity.Name);
                        new_doc_cmd.Parameters.AddWithValue("@assnmt_doc_upld_dt", DateTime.Now.ToString());
                        new_doc_cmd.Parameters.AddWithValue("@assnmt_doc_path", Server.MapPath("~/Assignments/assignment_docs/") + filename);
                        new_doc_cmd.Parameters.AddWithValue("@assnmt_doc_desc", assnmt_doc_desc_txt.Text);
                        new_doc_cmd.Parameters.AddWithValue("@assnmt_id_fk", hdn_assnmt_id_fk.Value);
       
                        azunl_cnxn.Open();
                        new_doc_cmd.ExecuteNonQuery();
                        azunl_cnxn.Close();
       
                        if (IsPostBack)
                        {
                            assnmt_doc_title_txt.Text = "";
                            assnmt_doc_type_ddl.SelectedValue = "";
                            assnmt_doc_desc_txt.Text = "";
                            hdn_assnmt_id_fk.Value = "";
       
                            gv_assnmt_docs.DataBind();
                            lbl_new_doc_submitted.Text = ("Your new document has been successfully submitted.");
                           
                        }
                    }           
                }
    This works just fine on my pc, but I get the error in production.

    Thanks,
    J
     
  2. Could this be caused by a setting in IIS?
     
  3. Elshadriel

    Elshadriel Winhost Staff

    Not that I can think of. Have you tried verifying that you can write to the path with simpler code? Something like:

    Code:
    String path = Server.MapPath("~/Assignments/assignment_docs/") + "test.txt";
    Response.Write(path);
    File.Create(path);
    
     
    Michael likes this.
  4. Thank for the assistance. Unfortunately I get the same type of error...
    But when I tested it locally (C: ), it saved just fine to the "\asignment_docs\" folder. It appears to only be an issue on when I move it to the Winhost server.
     
  5. I'm not sure why, but I could see the assignment_docs folder on the production server, but I wasn't able to explore it - it wasn't found. So I just sent all of the files over to the server again and after 2 attempts it now sees the folder and it works as intended.
     
    ComputerMan, Michael and Elshadriel like this.

Share This Page