我正在尝试上传文件而无法将其转换为。在我的实际页面上的“UploadStatusLabel”中生成以下错误:
“上传状态:无法上传文件。发生以下错误:对象引用未设置为对象的实例。”
这是背后的代码:
if (FileUpload1.HasFile)
{
try
{
if (FileUpload1.PostedFile.ContentType == "application/doc" ||
FileUpload1.PostedFile.ContentType == "appl/text" ||
FileUpload1.PostedFile.ContentType == "application/vnd.msword" ||
FileUpload1.PostedFile.ContentType == "application/vnd.ms-word" ||
FileUpload1.PostedFile.ContentType == "application/winword" ||
FileUpload1.PostedFile.ContentType == "application/word" ||
FileUpload1.PostedFile.ContentType == "application/msword" ||
FileUpload1.PostedFile.ContentType == "application/x-msw6" ||
FileUpload1.PostedFile.ContentType == "application/x-msword" ||
FileUpload1.PostedFile.ContentType == "application/pdf" ||
FileUpload1.PostedFile.ContentType == "application/x-pdf" ||
FileUpload1.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ||
FileUpload1.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.template"
)
{
if (FileUpload1.PostedFile.ContentLength < 102400000)
{
string filename = Path.GetFileName(FileUpload1.FileName);
string section = ddlSection.SelectedValue
FileUpload1.SaveAs(Server.MapPath("~/docs/HRDocs") + filename);
UploadStatusLabel.Text = "Upload status: Complete!";
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\webvideos.mdb;";
string cmdstr = "INSERT INTO Docs (Filename, Label, Section) VALUES (?,?,?)";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(cmdstr, con);
con.Open();
com.Parameters.AddWithValue("@Filename", filename);
com.Parameters.AddWithValue("@Label", txtDocLabelText.Text);
com.Parameters.AddWithValue("@Section", ddlSection.SelectedValue);
com.ExecuteNonQuery();
con.Close();
Response.Redirect("ManageHRDocs.aspx");
}
else
UploadStatusLabel.Text = "Upload status: The file has to be less than 100 MB!";
}
else
UploadStatusLabel.Text = "Upload status: Not an accepted file type";
}
catch (Exception ex)
{
UploadStatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
这是标记:
<asp:FormView ID="Formview1" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource1" DefaultMode="Insert">
<InsertItemTemplate>
Label:
<asp:TextBox ID="LabelTextBox" runat="server" />
<br />
Section:
<asp:DropDownList ID="ddlSection" runat="server"
DataSourceID="AccessDatasource2" DataTextField="Sections" DataValueField="Sections" />
<br /><br />
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:Button ID="UploadButton" runat="server" Text="Upload document" OnClick="UploadFile" /><br />
<asp:Label ID="UploadStatusLabel" runat="server" Text="Upload Status: " />
</InsertItemTemplate>
</asp:FormView>
页面本身加载正常,就在我尝试上传文档时,无论是pdf还是docx。另外,如果我尝试上传除了列出的文件以外的任何类型的文件,那么上传状态标签会正确更新,所以它确实似乎正在通过验证。
答案 0 :(得分:1)
当我在上面的代码中声明下拉列表时,我只是输入了错误的控件ID。我想这就是我没有发布我的整个代码所得到的......这是有效的代码。
代码隐藏:
string filename = Path.GetFileName(FileUpload1.FileName);
string section = ddlSection.SelectedValue;
string label = txtDocLabelText.Text;
FileUpload1.SaveAs(Server.MapPath("~/docs/HRDocs") + filename);
UploadStatusLabel.Text = "Upload status: Complete!";
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\webvideos.mdb;";
string cmdstr = "INSERT INTO Docs (Filename, Label, Section) VALUES (?,?,?)";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(cmdstr, con);
con.Open();
com.Parameters.AddWithValue("@Filename", filename);
com.Parameters.AddWithValue("@Label", label);
com.Parameters.AddWithValue("@Section", section);
com.ExecuteNonQuery();
con.Close();
Response.Redirect("ManageHRDocs.aspx");
标记:
<asp:FormView ID="Formview1" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource1" DefaultMode="Insert">
<InsertItemTemplate>
Label:
<asp:TextBox ID="LabelTextBox" runat="server" />
<br />
Section:
<asp:DropDownList ID="ddlSection" runat="server"
DataSourceID="AccessDatasource2" DataTextField="Sections" DataValueField="Sections" />
<br /><br />
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:Button ID="UploadButton" runat="server" Text="Upload document" OnClick="UploadFile" /><br />
<asp:Label ID="UploadStatusLabel" runat="server" Text="Upload Status: " />
</InsertItemTemplate>
</asp:FormView>
答案 1 :(得分:0)
也许,PostedFile属性为null。 自从我使用fileupload控件以来已经有一段时间了,但是如果我没有弄错的话,当你提交页面时,发布的文件可能会丢失。