将MS Word .doc存储在数据库中,然后再将其取出

时间:2010-07-28 20:47:34

标签: c# linq-to-sql ms-word

要在SQL中存储doc这个词我有:

        byte[] bytes = new byte[uploader.UploadedFiles[0].InputStream.Length];
        var storedFile = new email_attachment();
        string strFullPath = uploader.UploadedFiles[0].FileName;
        string strFileName = Path.GetFileName(strFullPath);

        storedFile.email_attachment_id = Guid.NewGuid();
        storedFile.emailer_id = new Guid(dropMailers.SelectedValue);
        storedFile.file_name = strFileName;
        storedFile.file_data = bytes;
        db.email_attachments.InsertOnSubmit(storedFile);
        db.SubmitChanges();

为了得到它,我用这个:

 var storedFile = db.email_attachments.Where(a => a.email_attachment_id.ToString() == dropAttachments.SelectedValue).Single();

        string strPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Storage/Email/Attachments");

        File.WriteAllBytes(Path.Combine(strPath, storedFile.file_name), storedFile.file_data.ToArray());
        System.IO.FileInfo file = new System.IO.FileInfo(Path.Combine(strPath, storedFile.file_name));

        if (file != null && file.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.End();
        }
        else
        {

            //Response.Write("This file does not exist.");

        }

问题:我回来的文档是空白的,我也相信将它保存在文件系统中,然后将其写入响应流并不是很有效。我在想这个步骤可以跳过。任何帮助或信息将不胜感激。

2 个答案:

答案 0 :(得分:1)

让我们看一下这段代码:

byte[] bytes = new byte[uploader.UploadedFiles[0].InputStream.Length];
var storedFile = new email_attachment();
string strFullPath = uploader.UploadedFiles[0].FileName;
string strFileName = Path.GetFileName(strFullPath);

storedFile.email_attachment_id = Guid.NewGuid();
storedFile.emailer_id = new Guid(dropMailers.SelectedValue);
storedFile.file_name = strFileName;
storedFile.file_data = bytes;
db.email_attachments.InsertOnSubmit(storedFile);
db.SubmitChanges();

我可以看到你创建一个空数组 - 但我无法看到你在哪里填充它。你不应该从InputStream读取而不是仅仅找出它的长度吗?

(您可能希望从输入流复制到MemoryStream,然后使用ToArray在最后获取一个字节数组。有大量示例代码用于复制流 - 在.NET 4中,甚至还有WriteTo方法,这使得它变得更容易。)

我还没有看过后面的代码 - 首先让我们确保你实际上先将一些数据输入数据库......

答案 1 :(得分:0)

写入db:

                byte[] bytes = new byte[uploader.UploadedFiles[0].InputStream.Length];
                uploader.UploadedFiles[0].InputStream.Read(bytes, 0, bytes.Length);

                var storedFile = new document();
                string strFullPath = uploader.UploadedFiles[0].FileName;
                string strFileName = Path.GetFileName(strFullPath);

                storedFile.document_id = Guid.NewGuid();
                storedFile.content_type = uploader.UploadedFiles[0].ContentType;
                storedFile.original_name = strFileName;
                storedFile.file_data = bytes;
                storedFile.date_created = DateTime.Now;
                db.documents.InsertOnSubmit(storedFile);
                db.SubmitChanges();

并将文件退回:

var storedFile = db.documents.Where(a => a.document_id.ToString() == dropAttachments.SelectedValue).Single();

            Response.Clear();
            Response.ContentType = "application/x-unknown";
            Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + storedFile.original_name + "\"");
            Response.BinaryWrite(storedFile.file_data.ToArray());
            Response.End();

在这里找到我的答案:http://imar.spaanjaars.com/414/storing-uploaded-files-in-a-database-or-in-the-file-system-with-aspnet-20#Downloads