我正在使用HTML标签制作文件上传器。我的数据表中有一列,其数据类型为varbinary(max)
,因此文件已成功以二进制格式保存在数据库中。我还在网格中显示文件列表,旁边有一个图标,用于查看文件。当我单击该图标时,它应该读取二进制格式的数据,然后查看该文件。
我要在后面的代码上载的代码是:
protected void Upload(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "insert into FileUploader2 values (@Name, @ContentType, @Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Context.Response.Write("Uploaded Successfully!");
}
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
我也能够成功读回我的数据。其代码如下:
[System.Web.Services.WebMethod]
public static void ShowDocument()
{
string filename = string.Empty;
string contentType = string.Empty;
byte[] bytes = null;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
using (SqlCommand com = new SqlCommand("SELECT * FROM FileUploader2", con))
{
using (SqlDataReader reader = com.ExecuteReader())
{
if (reader.Read())
{
filename = (string)reader["Name"];
contentType = (string)reader["ContentType"];
bytes = (byte[])reader["Data"];
}
}
}
}
System.Web.HttpContext.Current.Response.ContentType = contentType;
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;
filename=" + filename);
System.Web.HttpContext.Current.Response.OutputStream.Write(bytes, 0, bytes.Length);
System.Web.HttpContext.Current.Response.Flush();
}
现在阅读后,由于我正在使用附件,它还应该显示文件内容或下载文件的链接。但是它什么也没做。调试后给我正确的值。但是为什么不显示文件呢?我究竟做错了什么?据我所知,由于我正在使用内容处置,因此它应该独立显示。
我认为...您缺少该部分...
string strFileName = "FileName";//Here you have to set the File Name...
if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "pdf")
{
Response.ContentType = "application/PDF";
}
else if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "doc")
{
Response.ContentType = "application/msword";
}
else if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "docx")
{
Response.ContentType = "application/msword";
}
else if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "xls")
{
Response.ContentType = "application/vnd.ms-excel";
}
else if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "xlsx")
{
Response.ContentType = "application/vnd.ms-excel";
}
else if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "ppt")
{
Response.ContentType = "application/vnd.ms-powerpoint";
}
else if (strFileName.Substring(strFileName.IndexOf('.') + 1).ToLower() == "txt")
{
Response.ContentType = "text/plain";
}
else
{
Response.ContentType = "text/plain";
}
System.IO.FileInfo file = new System.IO.FileInfo(strFileName);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.WriteFile(file.FullName);
Response.End();