簡體   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