如何从GridView中的目录文件夹下载.doc,.pdf,.rtf文件 [英] How to download .doc, .pdf, .rtf files from directory folder within GridView

查看:82
本文介绍了如何从GridView中的目录文件夹下载.doc,.pdf,.rtf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨......朋友们,



在我的网络应用程序中管理员帖子工作用户面板用户可以申请上传简历文件的各种帖子(in。 doc,.pdf,.rtf格式)和一些细节。我已将恢复文件(.doc,.pdf,.rtf)保存在我的目录文件夹和file_path以及数据库表中的其他信息中。



然后我显示了应用的用户信息。在管理面板的GridView中,但现在我想在按钮点击时下载恢复文件....



我该怎么办...



我允许用户以.doc,.pdf,.rtf文件格式上传简历....



请帮助我...



谢谢

Hi...friends,

In my web application Admin Posts Jobs For which at user Panel User can apply for various posts with uploading resume file (in .doc, .pdf, .rtf formats only) and some details. I have save resume file (.doc, .pdf, .rtf) in my directory folder and file_path and other information in database table.

Then I displayed the applied user info. in GridView at admin panel but now i want to down load resume file at button click....

How can i do this...

I have allowed user to upload resume in .doc, .pdf, .rtf file formates....

Please help me...

Thanks

推荐答案

在Gridview中添加



< asp:templatefield headertext =Downloadxmlns:asp =#unknown>

< itemtemplate>

< asp:imagebutton id =btnDownloadrunat =servertooltip =Downloadimageurl =〜/ Images / icon / Download.ico>

CommandName =DownloadCommandArgument ='< ;%#Bind(ReportName)%>'/>



< itemstyle width =10%>







在行命令中添加以下代码



protected void gvReport_RowCommand(object sender,GridViewCommandEventArgs e)

{

尝试

{

if(e.CommandName ==Download)

{

string CommandArgument = e .CommandArgument.ToString();





string path =./;

string serverPath = HttpContext.Current.Server.MapPath(path);

string pdfpath = serverPath +QuarterlyReports\\+ CommandArgument +。pdf;



//Response.Redirect(fileName);

Response.ContentType =appl ication / pdf;

Response.AppendHeader(Content-Disposition,附件; filename =+ Path.GetFileName(pdfpath));

Response.TransmitFile(pdfpath);

//HttpContext.Current.ApplicationInstance.CompleteRequest();

Response.End();



}

catch(例外情况)

{



}
Add in Gridview

<asp:templatefield headertext="Download" xmlns:asp="#unknown">
<itemtemplate>
<asp:imagebutton id="btnDownload" runat="server" tooltip="Download" imageurl="~/Images/icon/Download.ico">
CommandName="Download" CommandArgument='<%# Bind("ReportName") %>' />

<itemstyle width="10%">



Add below code in row command

protected void gvReport_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "Download")
{
string CommandArgument = e.CommandArgument.ToString();


string path = "./";
string serverPath = HttpContext.Current.Server.MapPath(path);
string pdfpath = serverPath + "QuarterlyReports\\" + CommandArgument + ".pdf";

//Response.Redirect(fileName);
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(pdfpath));
Response.TransmitFile(pdfpath);
//HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.End();

}
catch (Exception ex)
{

}


protected void gd_cat_RowCommand(object sender,GridViewCommandEventArgs e)

{

if(e.CommandName ==downLoad)

{

DataTable dt = new DataTable();

Int32 id = Convert.ToInt32(e.CommandArgument);

p.Admin_Flag = 8;

p.Id = id;

p.Post_Date = System.DateTime.Now.Date;

dt = b.Job_Applied_GridShow(p);

if(dt.Rows.Count> 0)

{

string filepath =〜/+ dt.Rows [0] [file_path]。ToString();





// ........ ................................................ >
string path = Server.MapPath(filepath);

//从服务器获取物理文件路径

string name = Path.GetFileName(path);

//获取文件名

string ext = Path.GetExtension(path);

//获取文件扩展名

string type =;

//根据文件扩展名设置已知类型

if(ext!= null)

{

开关(ext.ToLower())

{

//案例.htm:

//案例.html:

// type =text / HTML;

// break;

// case.txt:

// type =text / plain;

// break;

// case .GIF:

// type =image / GIF;

// break;

case.pdf:

type =Application / pdf;

break;

case.doc:

case.rtf :

type =Application / msword;

break;

默认值:

type = ;

休息;

}

}

Response.AppendHeader(content-disposition,附件; filename =+ name);

if(type!=)

{

Response.ContentType = type;

Response.WriteFile(路径);

Response.End(); //为用户提供POP文件下载

}



}



}

}
protected void gd_cat_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "downLoad")
{
DataTable dt = new DataTable();
Int32 id = Convert.ToInt32(e.CommandArgument);
p.Admin_Flag = 8;
p.Id = id;
p.Post_Date = System.DateTime.Now.Date;
dt = b.Job_Applied_GridShow(p);
if (dt.Rows.Count > 0)
{
string filepath = "~/" + dt.Rows[0]["file_path"].ToString();


//.......................................................
string path = Server.MapPath(filepath);
//get physical file path from server
string name = Path.GetFileName(path);
//get file name
string ext = Path.GetExtension(path);
//get file extension
string type = "";
// set known types based on file extension
if (ext != null)
{
switch (ext.ToLower())
{
// case ".htm":
// case ".html":
// type = "text/HTML";
// break;
// case ".txt":
// type = "text/plain";
// break;
// case ".GIF":
// type = "image/GIF";
// break;
case ".pdf":
type = "Application/pdf";
break;
case ".doc":
case ".rtf":
type = "Application/msword";
break;
Default:
type = "";
break;
}
}
Response.AppendHeader("content-disposition", "attachment; filename=" + name);
if (type != "")
{
Response.ContentType = type;
Response.WriteFile(path);
Response.End(); //give POP to user for file downlaod
}

}

}
}


这篇关于如何从GridView中的目录文件夹下载.doc,.pdf,.rtf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆