C# 使用mvc 2010在SQL Server中上载/下载pdf文件
我一直在自学如何使用mvc 2010 Express在c#中编程,因为我被要求为我工作的公司创建一个应用程序。 我必须做的最后一件事是允许用户上传和下载pdf文件,但我找不到一个对我有帮助的解决方案 我使用的是SQL Server 2005。我要使用的表名为C# 使用mvc 2010在SQL Server中上载/下载pdf文件,c#,sql-server,asp.net-mvc,pdf,C#,Sql Server,Asp.net Mvc,Pdf,我一直在自学如何使用mvc 2010 Express在c#中编程,因为我被要求为我工作的公司创建一个应用程序。 我必须做的最后一件事是允许用户上传和下载pdf文件,但我找不到一个对我有帮助的解决方案 我使用的是SQL Server 2005。我要使用的表名为Clients。它有一个Id作为主键。我应该创建一个属性来将pdf存储为二进制文件吗?或者如何 要上传pdf,我的视图中有一个链接,可以将用户重定向到另一个视图(名为upload),该视图具有客户端的id。控制器名称为Home,因此用户将看到
Clients
。它有一个Id
作为主键。我应该创建一个属性来将pdf存储为二进制文件吗?或者如何
要上传pdf,我的视图中有一个链接,可以将用户重定向到另一个视图(名为upload),该视图具有客户端的id
。控制器名称为Home
,因此用户将看到如下内容:
Home.aspx/Upload/2
在那里,我希望用户能够选择他想要上传的pdf,然后点击按钮上传它。因此,控制器将使用[HttpPost]
处理它
要编辑客户机,这非常简单,因为我在ViewModels文件夹中创建了模型视图,然后将它们传递给控制器。
但是我如何将id
和pdf文件传递给控制器?我需要id
来知道那是什么用户。和如何将pdf存储在SQL Server中的我的表客户机中?
然后如何下载pdf?这就是我正在使用的…您应该做一些更改以使其适应您的需求 如何将id和pdf文件传递给控制器 查看:
@using (Html.BeginForm("Add", "Archivos",
FormMethod.Post, new { id = "attachment", enctype = "multipart/form-data", encoding = "multipart/form-data" }))
{
@Html.HiddenFor(x => Model.UserID)
<input type="file" name="uploadFile" id="uploadFile" />
<input type="submit" value="Guardar"/>
}
[HttpPost]
public ActionResult Add(HttpPostedFileBase uploadFile, int UserID)
{
if (uploadFile != null && uploadFile.ContentLength > 0)
{
//instance the user.. i.e "User1"
byte[] tempFile = new byte[uploadFile.ContentLength];
uploadFile.InputStream.Read(tempFile, 0, uploadFile.ContentLength);
User1.file.Content = tempFile;
User1.file.Save();
}
return RedirectToAction("Index");
}
CREATE TABLE [dbo].[FileTableName] (
[UserID] int NOT NULL,
[Name] varchar(256) NOT NULL,
[Type] varchar(256) NOT NULL,
[Length] int NOT NULL,
[Content] varchar(max) NOT NULL) // option: varbinary(max)
public class File
{
public string Name { get; set; }
public string Type { get; set; }
public long Length { get; set; }
public byte[] Content { get; set; }
}
public class User
{
public int UserID {get; set;}
public string name {get; set;}
/**/
public file file {get; set;}
/**/
public void SaveFile()
{
SqlDataReader _dataReader;
using (new MyConnectionManager())
{
using (_sqlCommand = new SqlCommand("SavePDFFile", MyConnectionManager.Connection))
{
_sqlCommand.CommandType = CommandType.StoredProcedure;
_sqlCommand.Parameters.Add(new SqlParameter("@UserID", this.UserID));
_sqlCommand.Parameters.Add(new SqlParameter("@Name", this.Name));
_sqlCommand.Parameters.Add(new SqlParameter("@Type", this.Type));
_sqlCommand.Parameters.Add(new SqlParameter("@Length", this.Length));
_sqlCommand.Parameters.Add(new SqlParameter("@Content", this.Content));
_dataReader = _sqlCommand.ExecuteReader();
_dataReader.Close();
}
}
}
public void LoadFile()
{
SqlDataReader _dataReader;
using (new MyConnectionManager())
{
using (_sqlCommand = new SqlCommand("GetPDFFIle", MyConnectionManager.Connection))
_sqlCommand.CommandType = CommandType.StoredProcedure;
_sqlCommand.Parameters.Add(new SqlParameter("@UserID", this.IDEnsayo));
_dataReader = _sqlCommand.ExecuteReader();
if (_dataReader.HasRows)
{
_dataReader.Read();
this.File.Name = (string)_dataReader["Name"];
this.File.Type = (string)_dataReader["Type"];
this.File.Length = (int)_dataReader["Length"];
this.File.Content = (byte[])_dataReader["Content"];
}
_dataReader.Close();
}
}
}
}
那我怎样才能下载pdf呢
控制器:
public ActionResult Get(int UserID)
{
var User1 = new User {UserID = UserID };
User1.LoadFile();
//If file exists....
MemoryStream ms = new MemoryStream(User1.File.Content, 0, 0, true, true);
Response.ContentType = User1.File.Type;
Response.AddHeader("content-disposition", "attachment;filename=" + User1.File.Name);
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
return new FileStreamResult(Response.OutputStream, User1.File.Type);
}
@Html.ActionLink("PDF", "Get", "Files", new { UserID = Model.UserID }, new { @class = "pdf-icon-l", title="Open PDF document" })
查看:
public ActionResult Get(int UserID)
{
var User1 = new User {UserID = UserID };
User1.LoadFile();
//If file exists....
MemoryStream ms = new MemoryStream(User1.File.Content, 0, 0, true, true);
Response.ContentType = User1.File.Type;
Response.AddHeader("content-disposition", "attachment;filename=" + User1.File.Name);
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
return new FileStreamResult(Response.OutputStream, User1.File.Type);
}
@Html.ActionLink("PDF", "Get", "Files", new { UserID = Model.UserID }, new { @class = "pdf-icon-l", title="Open PDF document" })
如何将pdf存储在SQL Server中的表客户端中
数据库表:
@using (Html.BeginForm("Add", "Archivos",
FormMethod.Post, new { id = "attachment", enctype = "multipart/form-data", encoding = "multipart/form-data" }))
{
@Html.HiddenFor(x => Model.UserID)
<input type="file" name="uploadFile" id="uploadFile" />
<input type="submit" value="Guardar"/>
}
[HttpPost]
public ActionResult Add(HttpPostedFileBase uploadFile, int UserID)
{
if (uploadFile != null && uploadFile.ContentLength > 0)
{
//instance the user.. i.e "User1"
byte[] tempFile = new byte[uploadFile.ContentLength];
uploadFile.InputStream.Read(tempFile, 0, uploadFile.ContentLength);
User1.file.Content = tempFile;
User1.file.Save();
}
return RedirectToAction("Index");
}
CREATE TABLE [dbo].[FileTableName] (
[UserID] int NOT NULL,
[Name] varchar(256) NOT NULL,
[Type] varchar(256) NOT NULL,
[Length] int NOT NULL,
[Content] varchar(max) NOT NULL) // option: varbinary(max)
public class File
{
public string Name { get; set; }
public string Type { get; set; }
public long Length { get; set; }
public byte[] Content { get; set; }
}
public class User
{
public int UserID {get; set;}
public string name {get; set;}
/**/
public file file {get; set;}
/**/
public void SaveFile()
{
SqlDataReader _dataReader;
using (new MyConnectionManager())
{
using (_sqlCommand = new SqlCommand("SavePDFFile", MyConnectionManager.Connection))
{
_sqlCommand.CommandType = CommandType.StoredProcedure;
_sqlCommand.Parameters.Add(new SqlParameter("@UserID", this.UserID));
_sqlCommand.Parameters.Add(new SqlParameter("@Name", this.Name));
_sqlCommand.Parameters.Add(new SqlParameter("@Type", this.Type));
_sqlCommand.Parameters.Add(new SqlParameter("@Length", this.Length));
_sqlCommand.Parameters.Add(new SqlParameter("@Content", this.Content));
_dataReader = _sqlCommand.ExecuteReader();
_dataReader.Close();
}
}
}
public void LoadFile()
{
SqlDataReader _dataReader;
using (new MyConnectionManager())
{
using (_sqlCommand = new SqlCommand("GetPDFFIle", MyConnectionManager.Connection))
_sqlCommand.CommandType = CommandType.StoredProcedure;
_sqlCommand.Parameters.Add(new SqlParameter("@UserID", this.IDEnsayo));
_dataReader = _sqlCommand.ExecuteReader();
if (_dataReader.HasRows)
{
_dataReader.Read();
this.File.Name = (string)_dataReader["Name"];
this.File.Type = (string)_dataReader["Type"];
this.File.Length = (int)_dataReader["Length"];
this.File.Content = (byte[])_dataReader["Content"];
}
_dataReader.Close();
}
}
}
}
为了存储文件,我使用了一个存储过程
型号:
@using (Html.BeginForm("Add", "Archivos",
FormMethod.Post, new { id = "attachment", enctype = "multipart/form-data", encoding = "multipart/form-data" }))
{
@Html.HiddenFor(x => Model.UserID)
<input type="file" name="uploadFile" id="uploadFile" />
<input type="submit" value="Guardar"/>
}
[HttpPost]
public ActionResult Add(HttpPostedFileBase uploadFile, int UserID)
{
if (uploadFile != null && uploadFile.ContentLength > 0)
{
//instance the user.. i.e "User1"
byte[] tempFile = new byte[uploadFile.ContentLength];
uploadFile.InputStream.Read(tempFile, 0, uploadFile.ContentLength);
User1.file.Content = tempFile;
User1.file.Save();
}
return RedirectToAction("Index");
}
CREATE TABLE [dbo].[FileTableName] (
[UserID] int NOT NULL,
[Name] varchar(256) NOT NULL,
[Type] varchar(256) NOT NULL,
[Length] int NOT NULL,
[Content] varchar(max) NOT NULL) // option: varbinary(max)
public class File
{
public string Name { get; set; }
public string Type { get; set; }
public long Length { get; set; }
public byte[] Content { get; set; }
}
public class User
{
public int UserID {get; set;}
public string name {get; set;}
/**/
public file file {get; set;}
/**/
public void SaveFile()
{
SqlDataReader _dataReader;
using (new MyConnectionManager())
{
using (_sqlCommand = new SqlCommand("SavePDFFile", MyConnectionManager.Connection))
{
_sqlCommand.CommandType = CommandType.StoredProcedure;
_sqlCommand.Parameters.Add(new SqlParameter("@UserID", this.UserID));
_sqlCommand.Parameters.Add(new SqlParameter("@Name", this.Name));
_sqlCommand.Parameters.Add(new SqlParameter("@Type", this.Type));
_sqlCommand.Parameters.Add(new SqlParameter("@Length", this.Length));
_sqlCommand.Parameters.Add(new SqlParameter("@Content", this.Content));
_dataReader = _sqlCommand.ExecuteReader();
_dataReader.Close();
}
}
}
public void LoadFile()
{
SqlDataReader _dataReader;
using (new MyConnectionManager())
{
using (_sqlCommand = new SqlCommand("GetPDFFIle", MyConnectionManager.Connection))
_sqlCommand.CommandType = CommandType.StoredProcedure;
_sqlCommand.Parameters.Add(new SqlParameter("@UserID", this.IDEnsayo));
_dataReader = _sqlCommand.ExecuteReader();
if (_dataReader.HasRows)
{
_dataReader.Read();
this.File.Name = (string)_dataReader["Name"];
this.File.Type = (string)_dataReader["Type"];
this.File.Length = (int)_dataReader["Length"];
this.File.Content = (byte[])_dataReader["Content"];
}
_dataReader.Close();
}
}
}
}
这真的应该是几个问题,所有这些都可以在SOThank you上找到!这个星期一我什么都试试。我真的很感激你花了一些时间阅读和回答我的问题,如此详细!我有几个问题。。。在第一个视图的控制器中,为什么它能识别UserId?存储PDF的表格称为
User
或file
?我还从未使用过存储过程。您在哪里告诉控制器要使用Save
存储pdf?这就是你在控制器中所说的SaveFile
?很抱歉问了这么多问题,但我有点不知所措。在第一个视图的控制器中,为什么它能识别UserId?因为您正在发布表单。在本例中,文件和ID已归档。存储PDF的表称为用户或文件?这是一个例子。这将取决于你的模型。在我的系统中,它被称为:“TestFile”。您在哪里告诉控制器要使用Save存储pdf?这就是你在控制器中所说的SaveFile吗?是的。我会更新我的答案。是的,我知道这是一个例子,但我想完全理解它,以便找到一个适合我的解决方案。。。我问错了问题。为什么要执行User1.file.Content
?TestFile
中不是内容吗?我认为您有一个名为User
的表,并且使用您“创建的”User1
的id,它的类型是User
。但是我不明白什么是文件
。您能告诉我这些表的结构吗?在varchar(max)
中存储pdf有效吗?我本以为应该是varbinary
列。