FileUpload.PostedFile.SaveAs() 无法在实时网站上运行 asp.net

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

我在网站上有一个表单,它可以做 2 件简单的事情。

  1. 重命名文件
  2. 将文件路由到服务器中 4 个特定文件夹之一。

当我 运行 它在 Visual Studio 中时,代码可以完美运行,但是,一旦它上线,它就无法运行。我没有收到任何错误或异常。我添加了一些脚本以进一步调试,并将问题缩小到调用 SaveAs() 的部分。我的猜测是,它与网站上线后的实际服务器路径有关。我试过同时使用 Server.MapPath() 和直接的物理路径,但没有成功。为了安全起见,我在这里没有提供带 IP 地址的实际物理路径,但再次说明一下,当我 运行 它在 Visual Studio 本地时,它工作得很好。

代码如下:

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);
    }
}

}

(原评论,略有格式)

你可以随时尝试

Response.Write(ex.ToString)

并检查客户端。记录它或其他任何东西 - 有多种方法可以做到这一点。或者相反,

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

并且没有 MapPath,以确保实际 filename/path 没有问题。

可能是 IIS 设置或其他问题。您可以检查它是否可访问等,然后将所有内容传回 Write 以在客户端查看它