文件无法使用c#ASP.NET保存在文件夹中
本文关键字:保存 存在 文件夹 NET c#ASP 文件 | 更新日期: 2023-09-27 18:00:43
我试图将文件保存在DB中,并使用c#保存在特定文件夹中,但我收到了以下错误:
错误:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=ODIYA_Doctor_Admin
StackTrace:
at ODIYA_Doctor_Admin.Module.Front_End_Management.banner.bnrSubmit_Click(Object sender, EventArgs e) in C:'ASP project'ODIYA_Doctor_Admin'ODIYA_Doctor_Admin'Module'Front_End_Management'banner.aspx.cs:line 82
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
我的代码:
banner.aspx:
<div class="col-md-6 bannerimagefile">
<label for="title" accesskey="T"><span class="required">*</span> Title</label>
<div id="errTitle" style="display:none;" runat="server"></div>
<asp:TextBox ID="txtBnrTitle" runat="server" size="30" value="" ></asp:TextBox>
<label for="status" accesskey="S"><span class="required">*</span> Status</label>
<asp:DropDownList ID="bnrStatus" runat="server" >
<asp:ListItem Text="Select your status" Value="enable" Selected="True"></asp:ListItem>
<asp:ListItem Text="Enable" Value="enable"></asp:ListItem>
<asp:ListItem Text="Disable" Value="disable"></asp:ListItem>
</asp:DropDownList>
<label for="insertimage" accesskey="B"><span class="required">*</span> Banner Image</label>
<asp:FileUpload runat="server" class="filestyle" data-size="lg" name="insertimage" id="FileUpload1" onchange="previewFile()" />
<label for="bannerimage" accesskey="V"><span class="required">*</span> View Image</label>
<div style="padding-bottom:10px;">
<asp:Image ID="bnrimgPhoto" runat="server" border="0" name="bannerimage" style="width:70px; height:70px;" />
</div>
<div class="clear"></div>
</div>
<div class="col-md-6">
<label for="shortdes" accesskey="V"><span class="required">*</span> Short Description</label>
<asp:TextBox ID="bnrshortdes" runat="server" size="30" value="" ></asp:TextBox>
<div id="bnrShtdesErrDiv" style="display:none;" runat="server"></div>
<label for="description" accesskey="D"><span class="required">*</span> Description</label>
<asp:TextBox ID="txtBnrDesc" runat="server" name="description" cols="40" Rows="7" TextMode="MultiLine"></asp:TextBox>
<div id="bnrDesErrDiv" style="display:none;" runat="server"></div>
<asp:Button runat="server" Text="Submit" class="submit" ID="bnrSubmit" OnClick="bnrSubmit_Click" />
</div>
</div>
</div>
</div>
banner.aspx。cs:
protected void bnrSubmit_Click(object sender, EventArgs e)
{
if (txtBnrTitle.Text.Trim().Length > 0 && bnrStatus.SelectedItem.Text.Length > 0 && FileUpload1.ClientID.Length > 0 && bnrshortdes.Text.Trim().Length > 0 && txtBnrDesc.Text.Trim().Length > 0)
{
string action = "";
if (bnrSubmit.Text == "Submit")
{
action = "insert";
}
if (bnrSubmit.Text == "Update")
{
action = "update";
}
objBannerBO.title = txtBnrTitle.Text.Trim();
objBannerBO.status = bnrStatus.SelectedValue.Trim();
string filename = FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(Server.MapPath(@"~'Upload'" + filename.Trim()));
string path = filename.Trim();
objBannerBO.image = path;
objBannerBO.shtdescription = bnrshortdes.Text.Trim();
objBannerBO.description = txtBnrDesc.Text.Trim();
objBannerBO.Created_By = Session["uid"].ToString();
if (action == "insert")
{
int result = objBannerBL.saveBannerDetails(objBannerBO, action);
if (result == 1)
{
Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Banner details Added Successfully!!')", true);
Response.Redirect("banner.aspx");
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Banner details could not Added !!')", true);
}
}
}
}
错误实际上出现在FileUpload1.PostedFile.SaveAs(Server.MapPath(@"~'Upload'" + filename.Trim()));
行。这里我需要将图像保存在文件夹和数据库中。请帮帮我。
试试这个:
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(Server.MapPath(@"~'Upload'" + filename));
}
如果您使用的是Updatepanel,那么您必须将Trigger添加到您的更新面板中。
<Triggers>
<asp:PostBackTrigger ControlID="bnrSubmit" />
</Triggers>
还将下面的行添加到Page_Load
Page.Form.Attributes.Add("enctype", "multipart/form-data");