SQL Server Management Studioにデータベースファイルを保存しています。私は自分のデータベースに格納されているPDFファイルを表示するgridviewコントロールを持っています。私はまた、対応するファイルを表示すると思われるリンクボタン "View"を持っています。私が持っている問題は、内容が私のリテラルコントロールに表示されていないということです。編集:私はまだ私のリテラルコントロールで表示する内容を取得する問題があります。エラーが表示されます。データが存在しないときに読み込みが無効です。私が得ているエラーは次の行です: bytes =(byte [])sdr ["Attachment"]; これ以外に何を試していいのか分かりませんが、これまでのコードはここにあります。私のリテラルコントロールにPDFコンテンツが表示されないのはなぜですか?
これは、QueryStringのIDを使用するハンドラです。 Fileデータをフェッチすることを想定しています。ここに私のハンドラのコードは次のとおりです。
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 [email protected]";
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内でクリックされたときに、私のLinkButtonコントロールを処理するための私のコードです:
: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);
}
私は絶対に運がないとこれをどこでも見てきました。私はsooooに感謝するどんな助けを!!!再度、感謝します!!!!!
どのようなリテラルコントロールですか?どの行がエラーをスローしますか? – David
この行でエラーが発生します。int id = int.Parse(context.Request.QueryString ["id"]); – user3474069
そして、 'context.Request.QueryString [" id "]'に何がありますか?このエラーは、それが「ヌル」であることを示唆しているようです。 – David