FileUpload.PostedFile.SaveAs() 在实时网站 asp.net 上不起作用
Posted
技术标签:
【中文标题】FileUpload.PostedFile.SaveAs() 在实时网站 asp.net 上不起作用【英文标题】:FileUpload.PostedFile.SaveAs() not working on live website asp.net 【发布时间】:2017-05-24 15:08:34 【问题描述】:我在一个网站上有一个表格,可以做两件简单的事情。
-
重命名文件
将文件路由到服务器中的 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);
【问题讨论】:
您可能遇到了异常,但没有注意到它,因为ex.Message
在字符串文字中无效。
@Slaks Nop,我把它从文字中取出并使用了一个简单的 Response.Write() 但我仍然没有得到任何异常。还有什么其他建议可以确保我没有遇到异常吗?
您可以随时尝试“Response.Write(ex.ToString) 并在客户端检查它。记录它或其他方式 - 多种方法。或者相反,Response.Write(Server.MapPath(destinationPath ) + newFileName)); 并且没有 MapPath,以确保实际文件名/路径没有问题。可能是 IIS 设置或其他问题。您可以检查它是否可访问等,然后将所有内容传回a 写到客户端看看。
@Aaron 谢谢你!终于根据你的建议解决了。原来这不是我的代码。你完全正确。管理员搞砸了 IIS 中的设置。如果您复制粘贴您的评论作为回复,我肯定会给您功劳。谢谢!!
Np,很高兴你得到它。我使用 Azure,我一直在客户端进行故障排除......我回家后会发布答案。
【参考方案1】:
(原为评论,略带格式)
你可以试试
Response.Write(ex.ToString)
并检查它的客户端。记录它或其他任何东西 - 多种方法来做到这一点。或者反过来,
Response.Write(Server.MapPath(destinationPath) + newFileName));
并且没有 MapPath,以确保实际文件名/路径没有问题。
也许这是一个 IIS 设置或什么的。您可以检查它是否可访问等,然后将所有内容传回 Write 以查看客户端
【讨论】:
以上是关于FileUpload.PostedFile.SaveAs() 在实时网站 asp.net 上不起作用的主要内容,如果未能解决你的问题,请参考以下文章