我正在使用fileupload控制器,并将文件数据保存为数据库表中的二进制文件,但是当我单击Save Changes按钮时它什么也没做,显示没有日志,没有错误,我甚至完全允许IUSR,但仍然不知道什么是什么出错了。这是代码。
<div class="form-horizontal">
<h4>Notifications</h4>
<hr />
<div class="form-group">
<div class="col-md-10 col-sm-10 col-xs-10">
<input id="Title" runat="server" placeholder="Enter Notification Titile" type="text" class="form-control" />
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-sm-10 col-xs-10">
<asp:FileUpload ID="FileUpload1" runat="server" />
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-sm-10 col-xs-10">
<input id="Discription" runat="server" placeholder="Enter Notification Discription" type="text" class="form-control" />
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-sm-10 col-xs-10">
<input id="Date" runat="server" placeholder="Notification Date" type="text" class="form-control" />
</div>
</div>
<asp:label runat="server" ID="Msg"></asp:label>
<div class="form-group">
<div class="col-md-10 col-sm-10 col-xs-10">
<asp:Button ID="Button1" CssClass="btn btn-primary" Text="Save Changes" runat="server"/>
</div>
</div>
</div>
protected void Button1_Click(object sender, EventArgs e)
{
DbCon obj = new DbCon();
SqlConnection con = obj.dbconnection();
string title = NotificationsTitle.Value;
string discription = NotificationDiscription.Value;
string date = NotificationDate.Value;
// Read the file and convert it to Byte Array
string filePath = FileUpload1.PostedFile.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
string contenttype = String.Empty;
//Set the contenttype based on File Extension
switch (ext)
{
case ".doc":
contenttype = "application/vnd.ms-word";
break;
case ".docx":
contenttype = "application/vnd.ms-word";
break;
case ".xls":
contenttype = "application/vnd.ms-excel";
break;
case ".xlsx":
contenttype = "application/vnd.ms-excel";
break;
case ".jpg":
contenttype = "image/jpg";
break;
case ".png":
contenttype = "image/png";
break;
case ".gif":
contenttype = "image/gif";
break;
case ".pdf":
contenttype = "application/pdf";
break;
}
try
{
if (contenttype != String.Empty)
{
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
con.Open();
//insert the file into database
string strQuery = "insert into Notifications(NotificationTitle,NotificationAttachment,NotificationDiscription,NotificationDate,NotificationStatus, NotificationContentType) Values(@title,@data,@dis,@date,@status,@type)";
SqlCommand cmd = new SqlCommand(strQuery, con);
cmd.Parameters.AddWithValue("@title", filename);
cmd.Parameters.AddWithValue("@data", bytes);
cmd.Parameters.AddWithValue("@dis", discription);
cmd.Parameters.AddWithValue("@date", date);
cmd.Parameters.AddWithValue("@status", "ACTIVE");
cmd.Parameters.AddWithValue("@type", contenttype);
cmd.ExecuteNonQuery();
NotificationMsg.Text = "File Uploaded Successfully";
}
else
{
NotificationMsg.ForeColor = System.Drawing.Color.Red;
NotificationMsg.Text = "File format not recognised." +
" Upload Image/Word/PDF/Excel formats";
}
}
catch (Exception ex)
{
NotificationMsg.ForeColor = System.Drawing.Color.Red;
NotificationMsg.Text = "File format not recognised." + ex.ToString();
}
finally
{
con.Close();
}
}