一般处理文件:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using HJing.MyBatis.OceanForecast.Domain;
using HJing.MyBatis.OceanForecast.Service;
using System.IO;
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int id = Convert.ToInt32(context.Request.QueryString["pdfid"]);
ReportService rservice = new ReportService();
ReportInfo info = rservice.GetById(id);
if (info != null)
{
info.DownloadCount++;
}
try
{
rservice.Update(info);
string filename = context.Server.MapPath(".." + info.Path);//去掉/字符
ResponseFile(context.Request, context.Response, filename.Substring(filename.LastIndexOf("\\") + 1), filename, 100);
}
catch
{
}
}
public bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
{
FileInfo myFile = new System.IO.FileInfo(_fullPath);
try
{
_Response.Clear();
_Response.AddHeader("Content-Disposition", "filename=" + myFile.Name);
_Response.AddHeader("Content-Length", myFile.Length.ToString());
System.Text.UnicodeEncoding unicode = new System.Text.UnicodeEncoding();
_Response.ContentEncoding = unicode;
_Response.ContentType = "Application/octet-stream";//"Application/pdf";这种形式是打开
_Response.WriteFile(_fullPath);
_Response.End();
}
catch (Exception e)
{
return false;
}
finally
{
}
return true;
}
public bool IsReusable
{
get
{
return false;
}
}
}
aspnet后台文件(数据绑定):
private void BindData()
{
string startdate = dfStartDate.Text;
string enddate = dfEndDate.Text;
Hashtable ht = new Hashtable();
if (startdate.IndexOf("0001") < 0)///1/1 0:00:00")
ht.Add("PublishTimeStart", startdate);
if (enddate.IndexOf("0001") < 0)// != "0001/1/1 0:00:00")
ht.Add("PublishTimeEnd", enddate);
switch(Request.QueryString["classid"])
{
case "1":
ht.Add("ClassName", "系列报告");
break;
case "2":
ht.Add("ClassName", "专题研究");
break;
case "3":
ht.Add("ClassName", "理论研究");
break;
}
ht.Add("Author", txtAuthor.Text.Trim());
ht.Add("Title", txtTitleKey.Text.Trim());
ht.Add("ReportType", cbReportType.SelectedItem.Value);
ReportService _service = new ReportService();
IList<ReportInfo> models = _service.GetByHashTable(ht);
foreach (ReportInfo item in models)
{
item.Title = "<a href='.." + item.Path + "' target='_blank'>" + item.Title + "</a>";
item.Path = "<a href='Handler.ashx?pdfid="+item.ID+"'><img src='../resources/images/word.jpg'></a>";
}
storeResult.DataSource = models;
storeResult.DataBind();
PagingToolbar1.PageIndex = 0;
if (models.Count == 0)
X.Msg.Alert("警告", "没有记录!").Show();
}
aspnet前台:
<ext:GridPanel ID="GridPanel1" runat="server" Title="报表列表</font>" Margins="0 0 5 5"
Icon="UserSuit" Region="Center" Frame="true" StripeRows="true">
<Store>
<ext:Store ID="storeResult" runat="server">
<Reader>
<ext:JsonReader IDProperty="ID">
<Fields>
<ext:RecordField Name="ID" />
<ext:RecordField Name="Title" />
<ext:RecordField Name="Author" />
<ext:RecordField Name="PublishTime" />
<ext:RecordField Name="ReportType" />
<ext:RecordField Name="DownloadCount" />
<ext:RecordField Name="Path" />
</Fields>
</ext:JsonReader>
</Reader>
<AutoLoadParams>
<ext:Parameter Name="start" Value="0" Mode="Raw" />
<ext:Parameter Name="limit" Value="30" Mode="Raw" />
</AutoLoadParams>
</ext:Store>
</Store>
<ColumnModel ID="ColumnModel1" runat="server">
<Columns>
<ext:RowNumbererColumn Width="40px" Header="序号" Align="Center" />
<ext:Column ColumnID="Title" Header="标题" DataIndex="Title" Align="Center" Width="200px" />
<ext:Column ColumnID="Author" Header="撰写人" DataIndex="Author" Align="Center"/>
<ext:Column ColumnID="ReportType" Header="类型" DataIndex="ReportType" Align="Center" />
<ext:Column ColumnID="PublishTime" Header="发布日期" DataIndex="PublishTime" Align="Center" />
<ext:Column ColumnID="DownloadCount" Header="下载次数" DataIndex="DownloadCount" Align="Center" />
<ext:Column ColumnID="Path" Header="下载" DataIndex="Path" Align="Center" />
</Columns>
</ColumnModel>
<SelectionModel>
<ext:RowSelectionModel ID="RowSelectionModel1" runat="server">
</ext:RowSelectionModel>
</SelectionModel>
<View>
<ext:GridView ID="GridView1" runat="server" ForceFit="true" MarkDirty="false" />
</View>
<BottomBar>
<ext:PagingToolbar ID="PagingToolbar1" runat="server" PageSize="30" HideRefresh="true" />
</BottomBar>
<Listeners>
<RowClick Handler="verifyclick(this);" />
</Listeners>
<LoadMask ShowMask="true" />
</ext:GridPanel>