我正在SQL Server Management Studio中保存数据库文件。我有一个gridview控件,显示存储在我的数据库中的PDF文件。我还有一个链接按钮“View”,可以查看相应的文件。我遇到的问题是内容没有显示在我的文字控件中。编辑:我仍然无法在我的文字控件中显示内容。我收到错误:没有数据时无效尝试读取。我得到的错误是在这一行: bytes =(byte [])sdr [“Attachment”]; 我不太确定还有什么可以尝试,但这是我到目前为止的代码:
这是我调用的Handler,它使用QueryString中的ID。假设要获取文件数据。这是我的处理程序的代码:
public class FileCS : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int id = int.Parse(context.Request.QueryString["id"]);
byte[] bytes;
string fileName, contentType;
string constr = ConfigurationManager.ConnectionStrings["PALM3ConnectionString2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT Attachment FROM Support_Doc WHERE SupportDocID=@SupportDocID";
cmd.Parameters.AddWithValue("@SupportDocID", id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["Attachment"];
contentType = sdr["Name"].ToString();
fileName = sdr["Type"].ToString();
}
con.Close();
}
}
context.Response.Buffer = true;
context.Response.Charset = "";
if (context.Request.QueryString["download"] == "1")
{
context.Response.AppendHeader("Content-disposition", "attachment; filename=" + fileName);
}
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.ContentType = "application/pdf";
context.Response.BinaryWrite(bytes);
context.Response.Flush();
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
这是我在gridview中单击时处理链接按钮的代码:
protected void Button2_Click(object sender, EventArgs e)
{
//int id = int.Parse((sender as LinkButton).CommandArgument);
int id = Convert.ToInt32(Request.QueryString["SupportDocID"]);
string embed = "<object data=\"{0}{1}\" type=\"application/pdf\" width=\"500%\" height=\"600px\">";
embed += "If you are unable to view file, you can download from <a href = \"{0}{1}&download=1\">here</a>";
embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
embed += "</object>";
ltEmbed.Text = string.Format(embed, ResolveUrl("~/FileCS.ashx?Id="), id);
}
此代码将文件上传到我的数据库:
protected void Button1_Click(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);
using (SqlConnection con = Connection.GetConnection())
{
//using (SqlCommand com = new SqlCommand())
//{
// com.CommandText = "UploadDoc";
//com.CommandType = CommandType.StoredProcedure;
string query = "insert into Support_Doc values (@Type, @Name, @Attachment)";
using (SqlCommand com = new SqlCommand(query)){
com.Connection = con;
com.Parameters.AddWithValue("@Type", filename);
com.Parameters.AddWithValue("@Name", contentType);
com.Parameters.AddWithValue("@Attachment", bytes);
con.Open();
com.ExecuteNonQuery();
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Text = "File Uploaded Successfully!";
con.Close();
}
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
我一直在寻找这个,绝对没有运气。任何帮助我都会感激不尽!再次感谢!!!!!
答案 0 :(得分:0)
这一行(和其他人一样):
bytes = (byte[])sdr["Attachment"];
假设数据存在于数据读取器中。但是,如果此行返回false
:
sdr.Read();
然后没有找到记录。因此错误告诉您,您正在尝试读取不存在的记录。这类似于尝试读取空数组的第一个元素(emptyArray[0]
)。
您可以使用bool
的{{1}}返回值来确定在尝试读取该记录之前是否存在记录。如果您只期望一条记录(代码所暗示的),那么这样的事情应该有效:
sdr.Read()
if (sdr.Read())
{
bytes = (byte[])sdr["Attachment"];
contentType = sdr["Name"].ToString();
fileName = sdr["Type"].ToString();
}
else
{
// no record was found, how do you want to proceed?
}
块中的操作取决于您自己。可能会向用户返回错误消息?也许执行另一个查询以向用户返回一些“默认”PDF?别的什么?但是,您希望处理错误条件取决于您希望系统的行为方式。关键是存在潜在的错误情况,您需要检查它。
最终,正在请求一条不存在的记录。所以代码需要能够解释它。
答案 1 :(得分:-1)
int id = Convert.ToInt32(Request.QueryString["Id"]);
代替
int id = Convert.ToInt32(Request.QueryString["SupportDocID"]);