繁体   English   中英

如何使用asp.net下载pdf文件?

[英]How to download pdf file using asp.net?

我上传文件并存储在服务器的数据文件夹中

文件路径:

Project Name
 |_bin
 |_css
 |_Data
   |_Mohamedfaisal.pdf

这是我的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();
   }
  }
 }
}

前端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>

我的前端设计UI设计

当我单击该链接时,它显示以下错误消息

mscorlib.dll 中出现“System.IO.DirectoryNotFoundException”类型的异常,但未在用户代码中处理

附加信息:找不到路径“F:\\Visual Studio Project\\Expatriates\\Expatriates\\Data\\”的一部分。

上传文件工作正常,但我没有下载。

您的代码可能失败的唯一原因是e.CommandArgument没有有效的文件名。 我认为由于某种原因没有传递命令参数,请查看您的标记。

您必须像这样为LinkButton显式指定CommandArgument

CommandArgument='<%# Eval("File") %>'

您没有为 RowCommand 事件提供 commandArgument。 改变

<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" Text='<%# Eval("File") %>'></asp:LinkButton>

<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" CommandArgument='<%# Eval("File") %>' Text='<%# Eval("File") %>'></asp:LinkButton>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM