FileUpload.PostedFile.SaveAs() 在实时网站 asp.net 不起作用



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

  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 = @"\IPAddresse$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)

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

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

并且没有 MapPath,以确保实际文件名/路径没有问题。

也许是IIS设置或其他东西。您可以检查它是否可访问等,只需将所有内容传回 Write 即可在客户端查看

相关内容

  • 没有找到相关文章

最新更新