[英]How to download pdf file using asp.net?
I upload file and stored in Data folder in server我上传文件并存储在服务器的数据文件夹中
File path:文件路径:
Project Name
|_bin
|_css
|_Data
|_Mohamedfaisal.pdf
this is my asp.net code这是我的asp.net代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
namespace Expatriates {
public partial class FileUploadForm: System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
protected void Button1_Click(object sender, EventArgs e) {
if (FileUpload1.HasFile) {
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Data/") + FileUpload1.FileName);
}
DataTable dt = new DataTable();
dt.Columns.Add("File", typeof(string));
dt.Columns.Add("size", typeof(string));
dt.Columns.Add("type", typeof(string));
foreach(string strFile in Directory.GetFiles(Server.MapPath("~/Data/"))) {
FileInfo fi = new FileInfo(strFile);
dt.Rows.Add(fi.Name, fi.Length, fi.Extension);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
if (e.CommandName == "Download") {
Response.Clear();
Response.Write(e.CommandArgument);
Response.ContentType = "application/octect-stream";
Response.AppendHeader("content-disposition", "filename=" + e.CommandArgument);
Response.TransmitFile(Server.MapPath("~/Data/") + e.CommandArgument); // error occured
Response.End();
}
}
}
}
Front End asp.net前端asp.net
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUploadForm.aspx.cs" Inherits="Expatriates.FileUploadForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="font-family:Arial;">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="File">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" Text='<%# Eval("File") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Size" HeaderText="Size in Bytes" />
<asp:BoundField DataField="Type" HeaderText="File Type" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
my front end design UI design我的前端设计UI设计
when I click that link it shows that following error message当我单击该链接时,它显示以下错误消息
An exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll but was not handled in user code
mscorlib.dll 中出现“System.IO.DirectoryNotFoundException”类型的异常,但未在用户代码中处理
Additional information: Could not find a part of the path 'F:\\Visual Studio Project\\Expatriates\\Expatriates\\Data\\'.
附加信息:找不到路径“F:\\Visual Studio Project\\Expatriates\\Expatriates\\Data\\”的一部分。
Uploading file is working perfectly, But I am not getting a download.上传文件工作正常,但我没有下载。
The only reason your code could fail would be if e.CommandArgument
doesn't have a valid file name.您的代码可能失败的唯一原因是
e.CommandArgument
没有有效的文件名。 I think the Command Argument isn't being passed for some reason, please look into your markup.我认为由于某种原因没有传递命令参数,请查看您的标记。
You have to explicitly specify CommandArgument
for a LinkButton
like this:您必须像这样为
LinkButton
显式指定CommandArgument
:
CommandArgument='<%# Eval("File") %>'
You don't supply a commandArgument for your RowCommand event.您没有为 RowCommand 事件提供 commandArgument。 Change
改变
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" Text='<%# Eval("File") %>'></asp:LinkButton>
to到
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" CommandArgument='<%# Eval("File") %>' Text='<%# Eval("File") %>'></asp:LinkButton>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.