当前,我正在尝试通过FileUpload上传图像并将其存储在数据库中。我的问题是,当我单击“上传”按钮时,尝试上传时总是会收到System.NullReferenceException。
现在,我知道UpdatePanel中的asp-FileUploads存在问题-因此我将上载按钮设置为PostBackTrigger,这会导致完整的回发-但仍然无法正常工作。
那是我到目前为止所得到的:
<asp:UpdatePanel ID="Update_woundpic" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:PostBackTrigger ControlID="woundpic_upload" />
</Triggers>
<ContentTemplate>
<div class="col-md-3">
<h3 class="label2">Upload
<small class="text-muted">maximal 5MB</small>
</h3>
<asp:FileUpload ID="uploadWoundImage" runat="server" />
<br />
<asp:LinkButton ID="woundpic_upload" runat="server" OnClick="btn_Upload_Click" CssClass="btn3d btn btn-default btn-lg"><span aria-hidden="true" class="glyphicon glyphicon-upload"></span> Hochladen</asp:LinkButton>
<asp:Label ID="lbl_success" runat="server" Text="Upload erfolgreich!" CssClass="uploadSuccess" Visible="False"></asp:Label>
<asp:Label ID="lbl_error" runat="server" Text="Upload nicht erfolgreich!" CssClass="uploadFailure" Visible="False"></asp:Label>
<br />
<br />
</div>
</ContentTemplate>
</asp:UpdatePanel>
这是我上传图片/文件的方法:
protected void btn_Upload_Click(object sender, EventArgs e)
{
byte[] imgByte;
using(BinaryReader reader = new BinaryReader(uploadWoundImage.PostedFile.InputStream))
{
imgByte = reader.ReadBytes(uploadWoundImage.PostedFile.ContentLength);
}
DateTime imageUpload = DateTime.Now;
string imageFormat = uploadWoundImage.PostedFile.ContentType;
string imageName = Path.GetFileName(uploadWoundImage.PostedFile.FileName);
_db.SaveWoundImage(imgByte, imageFormat, imageName, imageUpload);
}
现在,如果我单击按钮行
using(BinaryReader reader = new BinaryReader(uploadWoundImage.PostedFile.InputStream))
抛出System.NullReferenceException,所以我不知道为什么即使完整回发也找不到该文件。
感谢您的帮助!