c# - 使用 mvc 2010 在 SQL Server 中上传/下载 pdf 文件

标签 c# sql-server asp.net-mvc pdf

我一直在自学如何使用 mvc 2010 Expressc# 中编程,因为我被要求为我工作的公司创建一个应用程序为了。 我要做的最后一件事是允许用户上传和下载 pdf 文件,但我找不到对我有帮助的解决方案。

我使用 SQL Server 2005。我要使用的表称为 Clients。它有一个 Id 作为主键。我应该创建一个属性来将 pdf 存储为二进制文件吗?或者如何?

要上传 pdf,我在我的 View 中有一个链接,该链接将用户重定向到另一个 View (名为上传),其中包含客户端的 id。 Controller 名称是 Home,因此用户将看到如下内容:

Home.aspx/Upload/2

在那里,我希望用户能够选择他想要上传的 pdf,然后单击按钮上传它。所以 Controller 将使用 [HttpPost] 处理它。

要编辑客户端非常简单,因为我在文件夹 ViewModels 中创建了模型 View ,然后将它们传递给 Controller ​​。 但是我怎样才能将 id 和 pdf 文件都传递给 Controller ​​?我需要 id 才能知道那是哪个用户。 如何将 pdf 存储在 SQL Server 中的表 Clients 中? 然后如何下载 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"/>

    }

Controller :

   [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");
    }

And then how can I download the pdf?

Controller :

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" })

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();
        }
    }
  }

 }

关于c# - 使用 mvc 2010 在 SQL Server 中上传/下载 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14523001/

相关文章:

c# - 使用 ViewBag 中的列表选择 TagHelper

sql-server - 从用于 Multi-Tenancy 的单一数据库/单一模式数据库架构开始

c# - 我可以在ASP.NET中显示存储过程的结果吗?

c# - 如何在MVC 4项目中制作显示模板

asp.net-mvc - MVC 路由中 Controller 的类别? (不同命名空间中的重复 Controller 名称)

asp.net-mvc - ASP.NET MVC 中外部 css 的更好方法?

c# - 并发执行有限数量的任务

c# - 在 linq 结果中包含 count = 0

c# - 如何将文本框数据导出到excel文件?

sql-server - 检查sql server中分层sql表是否存在子级