【问题标题】:Upload/download pdf files in SQL Server using mvc 2010使用 mvc 2010 在 SQL Server 中上传/下载 pdf 文件
【发布时间】:2025-12-22 20:25:11
【问题描述】:

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

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

要上传 pdf,我的视图中有一个链接,可将用户重定向到客户端的 id 的另一个视图(名为 Upload)。控制器名称是Home,所以用户会看到类似:

Home.aspx/Upload/2

在那里,我希望用户能够选择他要上传的 pdf,然后单击按钮上传。因此控制器将使用[HttpPost] 处理它。

编辑客户端非常简单,因为我在 ViewModels 文件夹中创建了模型视图,然后将它们传递给控制器​​。 但是我怎样才能将id 和pdf 文件都传递给控制器​​?我需要id 才能知道那是什么用户。 如何将 pdf 存储在 SQL Server 中的表 Clients 中? 然后如何下载 pdf?

【问题讨论】:

  • 这确实应该是几个问题,都可以在SO上找到

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


【解决方案1】:

这就是我正在使用的……你应该做一些改变以适应你的要求。

如何将 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");
    }

然后我该如何下载 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" })

我如何将 pdf 存储在 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();
        }
    }
  }

 }

【讨论】:

  • 非常感谢!这个星期一我会尝试一切。非常感谢您花一些时间阅读和回答我的问题并且如此详细!
  • 我有几个问题......在第一个视图的控制器中,为什么它会识别UserId?您存储 pdf 的表称为 Userfile?而且我还没有使用过存储过程。你在哪里告诉控制器你想使用Save来存储pdf?是您在控制器中调用的SaveFile 吗?很抱歉问了这么多问题,但我有点迷茫。
  • 在第一个视图的控制器中,为什么它会识别UserId?因为您正在发布表单。在这种情况下,文件和 ID 归档。 您存储 pdf 的表称为用户还是文件? 这是一个示例。这将取决于你的模型。在我的系统中,它被称为:“TestFile”。 您在哪里告诉控制器您要使用 Saveto 存储 pdf?是你在控制器中调用的 SaveFile 吗? 是的。我会更新我的答案。
  • 是的,我知道这是一个例子,但我想完全理解它,以便提出适合我的解决方案......我问错了问题。你为什么要User1.file.ContentContent不是TestFile吗?我以为您有一个名为User 的表,并且您“创建”的ID 为User1,其类型为User。但我不明白file 是什么。你能告诉我这些表的结构吗?
  • varchar(max) 中存储pdf 是否有效?我原以为应该是varbinary 专栏。