简体   繁体   English

从asp.net mvc 2应用程序将pdf文件存储在sql服务器中的最佳方法是什么?

[英]what is the best way to store pdf files in sql server from an asp.net mvc 2 application?

I am trying to store pdf files in sql server. 我正在尝试将pdf文件存储在sql服务器中。 I am using an asp.net mvc 2 application with sql server? 我正在使用带有SQL Server的ASP.NET MVC 2应用程序? Can you show me some links to code samples? 您能告诉我一些代码示例链接吗?

The answer will depend very much on what technology you use to access this SQL Server, whether you are using an ORM, etc... Because you haven't provided any such information in your question, let me illustrate an example of how to do this using plain ADO.NET. 答案很大程度上取决于您使用什么技术来访问此SQL Server,是否使用ORM等。由于您的问题中未提供任何此类信息,因此让我举例说明如何执行此操作使用纯ADO.NET。

You could start with a form: 您可以从以下表格开始:

<% using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { %>
    <label for="file">Select a pdf file:</label>
    <input type="file" name="file" id="file" />
    <input type="submit" value="Upload" />
<% } %>

and a controller action to handle the submission of this form: 以及处理此表单提交的控制器操作:

[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
    if (file != null && file.ContentLength > 0)
    {
        var fileName = Path.GetFileName(file.FileName);
        var extension = Path.GetExtension(fileName);
        var document = new byte[file.InputStream.Length];
        file.InputStream.Read(document, 0, document.Length);
        if (string.Equals(".pdf", extension, StringComparison.OrdinalIgnoreCase))
        {
            using (var conn = new SqlConnection(SomeConnectionString))
            using (var cmd = conn.CreateCommand())
            {
                conn.Open();
                cmd.CommandText = "INSERT INTO Documents (document) VALUES (@document);";
                cmd.Parameters.Add("@document", SqlDbType.Binary, 20).Value = document;
                cmd.ExecuteNonQuery();
            }
        }
    }
    return View();
}

Of course the SQL part should be externalized into a separate repository to avoid cluttering the controller action with such code. 当然,应该将SQL部分外部化到一个单独的存储库中,以避免使用此类代码使控制器操作混乱。 Also if you are using SQL Server 2008 you might take a look at the new FILESTREAM type which is more adapted to this sort of things. 另外,如果您使用的是SQL Server 2008,则可以看看新的FILESTREAM类型 ,它更适合这种情况。

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

相关问题 从 ASP.Net MVC 将 3+ GB 文件上传到 Azure Blob 存储的最佳方法是什么? - What is the best way to upload 3+ GB files to Azure Blob Storage from ASP.Net MVC? 从ASP.NET MVC应用程序连接到SQL Server - Connect to SQL Server from ASP.NET MVC application 什么是asp.net-mvc / SQL服务器中最好的方法,存储昂贵的计算数据并像stackoverflow一样快速提供 - what is the best way in asp.net-mvc / SQL server, to store expensive calculated data and serve it up fast like stackoverflow 在 Asp.Net MVC 中存储 Authorize 属性的用户列表的最佳方法是什么? - What is the best way to store Users list for a Authorize attribute in Asp.Net MVC? ASP.NET MVC - 在 POST/PUT(架构)上存储对象的最佳方式是什么 - ASP.NET MVC - What's the best way to store objects on POST/PUT (Architecture) 在Asp.Net MVC 5中存储图像的最佳方法 - Best way to store images in Asp.Net MVC 5 存储ASP.NET 4.5 MVC用户数据的最佳方法 - Best way to store ASP.NET 4.5 MVC user data 在asp.net应用程序中显示pdf文档的最佳方法 - Best way to display pdf document in asp.net application 为ASP.NET MVC Intranet应用程序实施Windows身份验证的最佳方法是什么? - What's the best way to implement Windows Authentication for an ASP.NET MVC intranet application? 在ASP.NET MVC中设计向导表单的最佳方法是什么 - what is the best way to design a wizard form in ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM