问题描述
我有一个简单的网络表单,它显示了存储在数据库中的文件列表和每个文件的下载链接
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="LLPIposAttachment.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>iPOS Attachment</title>
<style type="text/css">
body{
font-family: Arial;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>iPOS Attachments List</h2>
</div>
<asp:GridView ID="AttachmentList" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField datafield="ATTACH_NAME" HeaderText="File Name" />
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="DownloadFile" CommandArgument='<%# Eval("ATTACH_ID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lbMsg" runat="server"></asp:Label>
<!-- log part -->
<asp:Label ID="lbLog" runat="server"></asp:Label>
<!-- log part -->
</form>
</body>
</html>
但我想添加一种特殊情况。如果数据库中只有一个文件,我不想显示网格,而只想显示标签中的消息,即只有一个文件,并自动下载此文件。我尝试这样做,实际上已下载文件,但根本未呈现网络表单。
using System;
using System.Collections.Generic;
using System.Data.sqlClient;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LLPIposAttachment
{
public partial class Default : System.Web.UI.Page
{
string bujn;
string[] param;
protected void Page_Load(object sender,EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["BUJN"] != null)
{
bujn = Request.QueryString["BUJN"].ToString();
param = bujn.Split('_');
}
else
{
param = new string[2];
if (Request.QueryString["BU"] != null)
{
param[0] = Request.QueryString["BU"].ToString();
}
if (Request.QueryString["JN"] != null)
{
param[1] = Request.QueryString["JN"].ToString();
}
}
sqlConnection sqlCon = new sqlConnection(WebConfigurationManager.AppSettings["inputsqlCon"]);
sqlCon.open();
string inputsql2 = @"SELECT count(*)
FROM PA_REQ_NOTE_ATTACHMENTS atta join PA_REQ_INVOICES inv on atta.NOTE_ID = inv.NOTE join " + param[0] +
"_A_SALFLDG ldg on ldg.TREFERENCE = 'IP ' + CONVERT(varchar(12),INV_NO) and ldg.ACCNT_CODE = inv.supplier_ACCNT_CODE " +
"where inv.SUN_DB = '" + param[0] + "'and D_C = 'C' and ldg.JRNAL_NO = " + param[1];
sqlCommand sqCom2 = new sqlCommand(inputsql2,sqlCon);
sqlDataReader sqRead2 = sqCom2.ExecuteReader();
sqRead2.Read();
int rowCount = sqRead2.GetInt32(0);
sqlCon.Close();
if (rowCount > 1)
{
BindGrid();
}
else if (rowCount == 1)
{
try
{
sqlCon.open();
inputsql2 = @"SELECT atta.ATTACH_ID,atta.ATTACH_NAME
FROM PA_REQ_NOTE_ATTACHMENTS atta join PA_REQ_INVOICES inv on atta.NOTE_ID = inv.NOTE join " + param[0] +
"_A_SALFLDG ldg on ldg.TREFERENCE = 'IP ' + CONVERT(varchar(12),INV_NO) and ldg.ACCNT_CODE = inv.supplier_ACCNT_CODE " +
"where inv.SUN_DB = '" + param[0] + "'and D_C = 'C' and ldg.JRNAL_NO = " + param[1];
sqCom2 = new sqlCommand(inputsql2,sqlCon);
sqRead2 = sqCom2.ExecuteReader();
sqRead2.Read();
string id = sqRead2.GetString(0);
string name = sqRead2.GetString(1);
lbMsg.Text = "File " + name + " was downloaded";
byte[] bytes;
string fileName;
string constr = WebConfigurationManager.AppSettings["inputsqlCon"];
using (sqlConnection con = new sqlConnection(constr))
{
using (sqlCommand cmd = new sqlCommand())
{
cmd.CommandText = @"SELECT ATTACH_NAME,ATTACH FROM PA_REQ_NOTE_ATTACHMENTS where ATTACH_ID = @Id";
cmd.Parameters.AddWithValue("@Id",id);
cmd.Connection = con;
con.open();
using (sqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["ATTACH"];
fileName = sdr["ATTACH_NAME"].ToString();
}
con.Close();
}
}
Response.ClearContent();
Response.Charset = "";
string mimeType = MimeMapping.GetMimeMapping(fileName);
Response.ContentType = mimeType;
response.addheader("Content-disposition",string.Format("attachment;FileName={0}",fileName));
response.addheader("Content-Length",bytes.Length.ToString());
Response.BinaryWrite(bytes);
Response.Flush();
Response.Close();
}
catch (Exception e1)
{
lbLog.Text = e1.Message;
}
}
}
}
private void BindGrid()
{
sqlConnection sqlCon = new sqlConnection(WebConfigurationManager.AppSettings["inputsqlCon"]);
sqlCon.open();
//live sql
//string inputsql = @"SELECT atta.ATTACH_ID,atta.ATTACH_NAME
// FROM PA_REQ_NOTE_ATTACHMENTS atta join PA_REQ_INVOICES inv on atta.NOTE_ID = inv.NOTE join " + param[0] +
// "_A_SALFLDG ldg on ldg.TREFERENCE = CONVERT(varchar(12),INV_NO) and ldg.ACCNT_CODE = inv.supplier_ACCNT_CODE " +
// "where inv.SUN_DB = '" + param[0] + "' and ldg.JRNAL_NO = " + param[1];
//live sql
//test sql
string inputsql = @"SELECT atta.ATTACH_ID,atta.ATTACH_NAME
FROM PA_REQ_NOTE_ATTACHMENTS atta join PA_REQ_INVOICES inv on atta.NOTE_ID = inv.NOTE join " + param[0] +
"_A_SALFLDG ldg on ldg.TREFERENCE = 'IP ' + CONVERT(varchar(12),INV_NO) and ldg.ACCNT_CODE = inv.supplier_ACCNT_CODE " +
"where inv.SUN_DB = '" + param[0] + "'and D_C = 'C' and ldg.JRNAL_NO = " + param[1];
//test sql
sqlCommand sqCom = new sqlCommand(inputsql,sqlCon);
sqlDataReader sqRead = sqCom.ExecuteReader();
AttachmentList.DataSource = sqRead;
AttachmentList.DataBind();
sqlCon.Close();
}
protected void DownloadFile(object sender,EventArgs e)
{
string id = (sender as LinkButton).CommandArgument;
byte[] bytes;
string fileName;
string constr = WebConfigurationManager.AppSettings["inputsqlCon"];
using (sqlConnection con = new sqlConnection(constr))
{
using (sqlCommand cmd = new sqlCommand())
{
cmd.CommandText = @"SELECT ATTACH_NAME,ATTACH
FROM PA_REQ_NOTE_ATTACHMENTS
where ATTACH_ID = @Id";
cmd.Parameters.AddWithValue("@Id",id);
cmd.Connection = con;
con.open();
using (sqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["ATTACH"];
fileName = sdr["ATTACH_NAME"].ToString();
}
con.Close();
}
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
string mimeType = MimeMapping.GetMimeMapping(fileName);
Response.ContentType = mimeType;
Response.AppendHeader("Content-disposition","attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)