0

我目前正在研究使用 ASP.Net 和 C# 的 Web 应用程序将文档上传和下载到 sql server 2008 数据库的不同方法和技术。

我找到了这个excel 教程,我想知道它是否类似于以类似的方式上传 pdf 和 word 文档?

谢谢

4

4 回答 4

1

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.

于 2012-12-17T15:36:16.533 回答
1

本教程适用于任何文件,而不仅仅是 excel。关键在这部分:

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";

这里基本上发生的是文件流被转换为字节数组,该数组作为数据块存储在数据库中。这可用于任何文件类型。只要确保保留文件名(或至少扩展名),就像上面的示例一样,这样当您将其转回磁盘上的文件时,您就知道它是什么类型的文件。

于 2012-12-17T15:35:05.470 回答
0

答案很简单:是的,很相似。

编辑

您找到的链接是一个示例,向您展示了如何将文件内容存储到数据库中,它可以更改为任何其他不同的文件类型:pdf、doc、jpg 等等,只要您记录 mim e 类型,以便在下载时最终用户正确获取它

于 2012-12-17T15:32:15.597 回答
0

当您首先使用带有代码的 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();
                }
            }
于 2015-07-29T14:15:51.743 回答