【问题标题】:Downloading a file after storing it in SQL Server 2008 Data Base将文件存储在 SQL Server 2008 数据库中后下载文件
【发布时间】:2011-07-18 14:46:58
【问题描述】:

WEB 框架:C# 中的 ASP.NET

大家好,我在网上找了一些可以告诉我如何做到这一点的东西,但找不到。我有一个页面上传有关个人的信息,包括 doc docx 或 pdf 形式的简历。我可以上传文件,但我不知道如何在另一个页面上下载已加载到数据库中该人行的文件。这是上传代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    string filePath = FileUpload1.PostedFile.FileName;
    string filename = System.IO.Path.GetFileName(filePath);
    string ext = System.IO.Path.GetExtension(filename);
    string contenttype = String.Empty;

    switch (ext)
    {
        case ".doc":
            contenttype = "application/vnd.ms-word";
            break;
        case ".docx":
            contenttype = "application/vnd.ms-word";
            break;
        case ".pdf":
            contenttype = "application/pdf";
            break;
   }
    if (contenttype.Equals(String.Empty))
    {
        Response.Write(@"<script language='javascript'>alert('You have selected an invalid resume type.')</script>");
        return;
    }
    Stream fs = FileUpload1.PostedFile.InputStream;
    BinaryReader br = new BinaryReader(fs);
    Byte[] bytes = br.ReadBytes((Int32)fs.Length);

    SqlConnection myConnection = new SqlConnection("user id=;" + "password=;server=;" +
                                  "Trusted_Connection=yes;" +
                                  "database=contractors; " +
                                  "connection timeout=30");
     try
    {
        myConnection.Open();
    }
    catch (Exception el)
    {
        Console.WriteLine(el.ToString());
    }


    SqlCommand myCommand = new SqlCommand("Command String", myConnection);
    myCommand.CommandText = "INSERT INTO contractor (firstName, lastName,phone,email,company,location,fileName,contentType,data,date) " +
                    "Values (@sqlfirstName,@sqllastName,@sqlphone,@sqlemail,@sqlcompany,@sqllocation,@Name,@ContentType,@Data,@sqldate)";


    myCommand.Parameters.Add("@Name", System.Data.SqlDbType.VarChar).Value = filename;
    myCommand.Parameters.Add("@ContentType", System.Data.SqlDbType.VarChar).Value = contenttype;
    myCommand.Parameters.Add("@Data", System.Data.SqlDbType.Binary).Value = bytes;
    myCommand.Parameters.Add("@sqlfirstName", System.Data.SqlDbType.VarChar, 50).Value = TextBox1.Text;
    myCommand.Parameters.Add("@sqllastName", System.Data.SqlDbType.VarChar, 50).Value = TextBox2.Text;
    myCommand.Parameters.Add("@sqlemail", System.Data.SqlDbType.VarChar, 75).Value = TextBox4.Text;
    myCommand.Parameters.Add("@sqlphone", System.Data.SqlDbType.VarChar, 25).Value = TextBox3.Text;
    myCommand.Parameters.Add("@sqlcompany", System.Data.SqlDbType.VarChar, 30).Value = TextBox5.Text;
    myCommand.Parameters.Add("@sqllocation", System.Data.SqlDbType.VarChar, 70).Value = TextBox6.Text;
    myCommand.Parameters.Add("@sqldate", System.Data.SqlDbType.DateTime, 30).Value = DateTime.Now;


    myCommand.ExecuteNonQuery();
    try
    {
        myConnection.Close();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }

    Response.Write(@"<script language='javascript'>alert('You information has been submitted. Thank you.')</script>");

   }

 }

上传信息后,我有另一个页面,该页面使用网格视图向管理员用户显示信息。有人会帮我解决如何将其翻译回可下载文件吗?这是 ASP.net 代码

<div>
    <div class="article">

        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" emptydatatext="No data available"
            AllowSorting="True" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource1" 
            onselectedindexchanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:CommandField ShowSelectButton="True" SelectText="Get Resume" />
                <asp:BoundField DataField="firstName" HeaderText="First Name" 
                    SortExpression="firstName" />
                <asp:BoundField DataField="lastName" HeaderText="Last Name" 
                    SortExpression="lastName" />
                <asp:BoundField DataField="phone" HeaderText="Phone Number" 
                    SortExpression="phone" />
                <asp:BoundField DataField="email" HeaderText="Email" SortExpression="email" />
                <asp:BoundField DataField="company" HeaderText="Company" 
                    SortExpression="company" />
                <asp:BoundField DataField="location" HeaderText="Location" 
                    SortExpression="location" />
                <asp:BoundField DataField="fileName" HeaderText="fileName" 
                    SortExpression="fileName" Visible="False" />
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" 
                    ReadOnly="True" SortExpression="id" Visible="False" />
                <asp:BoundField DataField="date" HeaderText="date" SortExpression="date" />
                <asp:BoundField DataField="contentType" HeaderText="contentType" 
                    SortExpression="contentType" Visible="False" />
                </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:contractorsConnectionString %>" 
            SelectCommand="SELECT [firstName], [lastName], [phone], [email], [company], [location], [fileName], [id], [date], [data], [contentType] FROM [contractor]">
        </asp:SqlDataSource>

      </div>
      </div>

【问题讨论】:

  • Menas onclicking你要下载文件的文件名
  • 'Console' 在 asp.net 上不起作用

标签: c# asp.net sql-server file-upload file


【解决方案1】:

您尝试实现下载的方式存在一些问题和潜在问题。

使用通用处理程序读取文件并发送文件。

在 gridView 上添加指向通用处理程序的链接,并在查询时发送您要下载的文件的 ID。

例如&lt;a taget="_blank" href="/downloadFile.ashx?id=3"&gt;download&lt;/a&gt;

然后在ashx上读取3,打开数据库,然后发送文件。

还有

 Response.Write(@"<script language='javascript'>alert('You have selected an invalid resume type.')</script>");

这一行是不好的做法,请寻找其他方式向您的用户发送错误,并且不要使用 Respose.Write 在页面上呈现脚本。此外,此脚本可能会破坏您页面上的 DOM,并将在页面末尾的正文之后呈现。

控制台无法在 asp.net 上运行 - 请花更多时间了解页面呈现的内容和情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    相关资源
    最近更新 更多