简体   繁体   English

使用asp.net C#在sql server 2008中上传文档

[英]Uploading documents in sql server 2008 using asp.net C#

I'm currently researching different methods and techniques into uploading and downloading documents into a sql server 2008 database using ASP.Net and C# for a web application. 我正在研究使用ASP.Net和C#将文档上载和下载到sql server 2008数据库中的不同方法和技术,以用于Web应用程序。

I have found this excel tutorial , and i was wondering if it would be similar to upload pdf and word documents in a similar way? 我找到了这个excel教程 ,我想知道它是否类似于以类似的方式上传pdf和word文档?

Thanks 谢谢

This tutorial should work for any file, not just excel. 本教程适用于任何文件,而不仅仅是excel。 The key is in this part: 关键在于这一部分:

Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);  //reads the   binary files
Byte[] bytes = br.ReadBytes((Int32)fs.Length);  //counting the file length into bytes
query = "insert into Excelfiledemo(Name,type,data)" + " values (@Name, @type, @Data)"; //insert query
com = new SqlCommand(query, con);
com.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename1;
com.Parameters.Add("@type", SqlDbType.VarChar).Value = type;
com.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
com.ExecuteNonQuery();
Label2.ForeColor = System.Drawing.Color.Green;
Label2.Text = "File Uploaded Successfully";

What is basically happening here is that the file stream is being turned into a Byte array which is stored as a data blob in your database. 这里基本上发生的是文件流被转换为Byte数组,该数组作为数据blob存储在数据库中。 This can be used for ANY file type. 这可以用于任何文件类型。 Just be sure to keep the filename (or at least extension) around just as in the example above so that you know what kind of file it is when you turn it back into a file on disk. 请确保保留文件名(或至少扩展名),就像上面的示例一样,这样您就可以知道将文件转回磁盘上的文件时的文件类型。

Considering the example in the link you shared, use the following validation: 考虑您共享的链接中的示例,请使用以下验证:

switch (ext.ToLower())  
{
    case ".pdf": 
        type = "application/pdf"; 
        break;                 
    case ".doc":
        type = "application/msword";
        break;
    case ".docx":
        type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
}

Here are some more MS Office 2007 MIME types you may consider and on All MIME Types you may find a broader list. 以下是您可以考虑的更多MS Office 2007 MIME类型,在所有MIME类型上,您可以找到更广泛的列表。

The answer is simple: YES, it's similar. 答案很简单:是的,它很相似。

EDIT 编辑

The link you found is a sample shows you how to store a file content into database, it can be changed to any other different file type: pdf,doc,jpg, whatever, as long as you record the mim e type so that when download end user get it correctly 您找到的链接是一个示例,向您展示如何将文件内容存储到数据库中,它可以更改为任何其他不同的文件类型:pdf,doc,jpg,等等,只要您记录mim e类型以便下载时最终用户正确使用它

This answer is more appropriate when you are using ASP.NET MVC 4 or 5 with code first. 当您首先使用带有代码的ASP.NET MVC 4或5时,此答案更合适。

//on your view model
public class MyViewModel
    {
        [Required]
        [DisplayName("Select File to Upload")]
        public IEnumerable<HttpPostedFileBase> File { get; set; }
    }
// on your object class, make sure you have a data type byte[] that will store a file in a form of bytes, a byte[] data type store both Images and documents of any content type. 
    public class FileUploadDBModel
    {
        [Key]
        public int Id { get; set; }
        public string FileName { get; set; }
        public byte[] File { get; set; }
    }
// on your view 
<div class="jumbotron">
    @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <div>
            @Html.LabelFor(x => x.File)
            @Html.TextBoxFor(x => x.File, new { type = "file" })
            @Html.ValidationMessageFor(x => x.File)
        </div>
        <button type="submit">Upload File</button>
    }
</div>


//on your controller
 public ActionResult Index(MyViewModel model)
        {
                FileUploadDBModel fileUploadModel = new FileUploadDBModel();
                //uploading multiple files in the database
                foreach (var item in model.File)
                {
                    byte[] uploadFile = new byte[item.InputStream.Length];
                    item.InputStream.Read(uploadFile, 0, uploadFile.Length);

                    fileUploadModel.FileName = item.FileName;
                    fileUploadModel.File = uploadFile;

                    db.FileUploadDBModels.Add(fileUploadModel);
                    db.SaveChanges();
                }
            }

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

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