繁体   English   中英

使用mvc 2010在SQL Server上上载/下载pdf文件

[英]Upload/download pdf files in SQL Server using mvc 2010

我一直在学习如何使用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?

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

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

 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM