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>");
}
}
在上传信息后,我有另一个页面,使用gridview向管理员用户显示信息。有人会帮我解决如何将其翻译成可下载的文件吗?这是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>
答案 0 :(得分:2)
您尝试实施下载的方式有几个问题和潜在问题。
使用通用处理程序读取文件,然后发送文件。
在gridView上添加指向通用处理程序的链接,并在查询时发送您要下载的文件的ID。
例如<a taget="_blank" href="/downloadFile.ashx?id=3">download</a>
然后在ashx上读取3,然后打开数据库,然后发送文件。
Response.Write(@"<script language='javascript'>alert('You have selected an invalid resume type.')</script>");
这一行是不好的做法,找到一些向用户发送错误的方法,并且不要使用Respose.Write在页面上呈现脚本。此脚本也可能是goind打破页面上的DOM,并且将在页面末尾呈现在主体之后。
控制台无法在asp.net上运行 - 请花一些时间来了解最新情况以及页面渲染的内容。