Search code examples
c#asp.netvisual-studiosave-asserver.mappath

FileUpload.PostedFile.SaveAs() not working on live website asp.net


I have a form in a website that does 2 simple things.

  1. rename the file
  2. route the file to one of 4 specific folders in a server.

The code works perfectly when I run it inside Visual Studio, however, once it goes live it just doesn't work. I do not get any errors or exceptions. I've added some scripts to further debug and I have narrow the problem to the part where the SaveAs() is invoked. My guess is that it has to do with the actual server path once the site is live. I have tried using both the Server.MapPath() and just a straight physical path, but no luck. For security purposes, I am not providing the actual physical path with IP Address here, but once again suffice it to say, when I run it locally inside Visual Studio, it works perfectly.

Here's the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    //Server paths.
    private static string masterserverPath = @"\\IPAddress\e$\BatchImport\";
    private static string personalPath = masterserverPath + "HR PersonalFile\\";
    private static string benefitsPath = masterserverPath + "HR Benefits\\";
    private static string workCompPath = masterserverPath + "HR WC\\";
    private static string LOAPath = masterserverPath + "HR LOA\\";
    //values to dynamicaly populate 4 division in subdiv dropdown list
    string[] personalSubDivArr = {"Employment Inquiry", "EBI", "W-4", "Driver's Application", "Direct Deposit",
                            "Address Changes", "Name Changes", "Employment Verifications", "New Hire-Rehire Datasheet",
                            "PAF's Status Changes", "Acknoledgement of Wages for NY and CA", "Counseling",
                            "Record of Conversation", "Written Warning-Attachments", "Probationary Counseling-Attachments",
                            "Final Warning", "Final Incident Documentation", "Commendations", "Goals and Objectives",
                            "performance Reviews", "Associate Acknowledgements", "Associate Develepment Programs",
                            "Unemployment", "EEOC forms", "I-9"};

    string[] benefitsSubDivArr = {"Associate Benefits Enrollments", "Dependant Eligibility Verifications", "HIPPA Forms","Physicians Documents",
                            "Repayment Agreements", "Beneficiary forms", "Medical Claims", "Acknowledgement Forms", "Cancellation Forms"};

    string[] WCSubDivArr = {"First Report of Injury", "Workers Comp. Medical Claims", "Adjuster Notes", "Wage Statements Verifications",
                     "State Forms", "Medical Improvement Reports", "Release", "Miscellaneous"};

    string[] LOASubDivArr = {"Request for LOA", "Initial Letter", "Medical Certification", "Approval Letter", "Medical Updates", "Extensions",
                      "Release", "Return Letter", "Notes"};
    string newFileName;


protected void Page_Load(object sender, EventArgs e)
{

}

//Button1 fires up validations before uploading the file to the server
protected void Button1_Click(object sender, EventArgs e)
{

        //verify if a file to upload exists
        if (FileUpload.HasFile)
        {
            string fileExtension = System.IO.Path.GetExtension(FileUpload.FileName).ToLower();
            string[] allowedExtensions = {".pdf"};
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                //make sure document is of appropiate type **pdf in this case
                if (fileExtension == allowedExtensions[i])
                {
                    //make sure none of the fields are empty
                    if (string.IsNullOrWhiteSpace(LnameTextBox.Text) || string.IsNullOrWhiteSpace(FnameTextBox.Text)
                        || string.IsNullOrWhiteSpace(SSNTextBox.Text) || string.IsNullOrWhiteSpace(FileNoTextBox.Text)
                        || SubDivDropDownList.Text == "Select an option" || MasterDropDownList.Text == "Select an option")
                    {


                        UploadMssgLabel.Text = "Employee and Document info cannot be empty.";
                    }
                    else
                    {
                    //string to rename file
                        newFileName = (FnameTextBox.Text + " " + LnameTextBox.Text + " " +  SubDivDropDownList.Text 
                                        + " " + "FileNo-" +FileNoTextBox.Text + fileExtension);
                    Response.Write("<script>alert('Inside save');</script>");
                        try
                        {

                            //select which path to save the file to
                            string destinationPath;
                            switch (MasterDropDownList.Text)
                            {
                                case "Personal":
                                    destinationPath = personalPath;
                                    break;

                                case "Benefits":
                                    destinationPath = benefitsPath;
                                    break;

                                case "WorkersComp":
                                    destinationPath = workCompPath;
                                    break;

                                case "LOA":
                                    destinationPath = LOAPath;
                                    break;

                                default:
                                    destinationPath = null;
                                    break;
                            }

                        Response.Write("<script>alert('Saving "+ newFileName +"');</script>");
                        FileUpload.PostedFile.SaveAs(Server.MapPath(destinationPath) + newFileName); //I have tried with Server.MapPath() an without it
                        UploadMssgLabel.ForeColor = System.Drawing.Color.Green;
                        UploadMssgLabel.Text = "Upload Successful!";

                        }
                        catch (Exception ex)
                        {
                            Response.Write("<script>alert('" + ex.Message + "');</script>");
                        }
                    Response.Write("<script>alert('ending save');</script>");
                }

                }
                else
                {

                    UploadMssgLabel.Text = "Cannot accept files of this kind.";
                }
            }

        }
        else
        {

            UploadMssgLabel.Text = "Please select a file.";
        }

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    //Clear the subdiv dropdown every selection. Otherwise, items would just keep being added on
    SubDivDropDownList.Items.Clear();

    //switch to dynamicaly populate the subdiv dropdown
    switch (MasterDropDownList.SelectedValue)
    {
        case "Personal":
            populateDropDown(SubDivDropDownList, personalSubDivArr);
            break;

        case "Benefits":
            populateDropDown(SubDivDropDownList, benefitsSubDivArr);
            break;

        case "WorkersComp":
            populateDropDown(SubDivDropDownList, WCSubDivArr);
            break;

        case "LOA":
            populateDropDown(SubDivDropDownList, LOASubDivArr);
            break;

        default:
            SubDivDropDownList.Items.Add("Select an option");
            break;

    }
}

//Method to populate a dropdown with an array
private void populateDropDown(DropDownList list, Array arr)
{
    list.Items.Add("Select an option");

    foreach (string s in arr)
    {
        list.Items.Add(s);
    }
}

}

Solution

  • (originally a comment, slightly formatted)

    You can always try

    Response.Write(ex.ToString)
    

    and check it client side. log it or whatever - numerous ways to do it. Or conversely,

    Response.Write(Server.MapPath(destinationPath) + newFileName)); 
    

    and without the MapPath, to make sure there isn't a problem with the actual filename/path.

    Perhaps it's an IIS setting or something. You can check if it's accessible, etc, and just pass everything back in a Write to see it client side