我一直在学习如何使用mvc 2010 Express在c#中编程,因为我被要求为我工作的公司创建一个应用程序.
我要做的最后一件事是允许用户上传和下载pdf文件,但我找不到能帮助我的解决方案.
我要做的最后一件事是允许用户上传和下载pdf文件,但我找不到能帮助我的解决方案.
我使用sql Server 2005.我想使用的表称为Clients.它有一个Id作为主键.我应该创建一个属性来将pdf存储为二进制文件吗?或者怎么样?
要上传pdf,我的视图中有一个链接,用户将id重定向到另一个视图(名为Upload).控制器名称为Home,因此用户将看到如下内容:
Home.aspx/Upload/2
在那里,我希望用户能够选择他想要上传的PDF,然后单击按钮上传它.因此控制器将使用[HttpPost]处理它.
编辑客户端非常简单,因为我在viewmodels文件夹中创建了模型视图,然后将它们传递给控制器.
但是如何将id和pdf文件传递给控制器?我需要id知道用户是什么.如何将pdf存储在我的sql Server中的客户端?
然后我该如何下载pdf?
解决方法
这就是我正在使用的……你应该做一些改变以使其适应你的要求.
how can I pass to the controller both the id and the pdf file?
视图:
@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,uploadFile.ContentLength); User1.file.Content = tempFile; User1.file.Save(); } return RedirectToAction("Index"); }
And then how can I download the pdf?
控制器:
public ActionResult Get(int UserID) { var User1 = new User {UserID = UserID }; User1.LoadFile(); //If file exists.... MemoryStream ms = new MemoryStream(User1.File.Content,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(),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" })
And how can I store the pdf in my table Clients in sql Server?
数据库表:
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(); } } } }