上传的docx文件越来越损坏 [英] Uploaded Docx Files are getting corrupted

查看:367
本文介绍了上传的docx文件越来越损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的一个内部网站,允许用户将文档和存储文件上传到SQL 2008数据库。我们遇到的问题是,DOCX总是说,当你试图打开他们已损坏。单击确定后,他们又继续开罚款。我做错了什么? (所有其他类型的文件保存好,包括.doc)

上传code:

  //保存文件
        串mime类型= Request.Files [I] .ContentType;
        字符串文件路径= Path.GetFileName(Request.Files [I] .FileName);
        字符串文件名= Path.GetFileName(Request.Files [I] .FileName);
        流FILESTREAM = Request.Files [I] .InputStream;
        长文件长度= Request.Files [I] .InputStream.Length;        如果(文件长度大于0)
        {
            字节[] = FILEDATA新的字节[文件长度]
            fileStream.Read(FILEDATA,0,(int)的文件长度);
            _fileRepo.SaveFileToDatabase(_currentUser.Username,t.EncounterId,文件名,文件路径,mime类型,FILEDATA);
        }

code是将其插入到数据库中。

  tblDocumentImage IMG =新tblDocumentImage();
        img.DocumentImage =文件; // DB数据类型字段=图像...
...        _ctx.tblDocumentImage.InsertOnSubmit(IMG);
        _ctx.SubmitChanges();

code供应文件退了出去。

 公开文件DocumentById(INT的docID)
    {
        文档DOC =新的文件(docID的;
...
            变种F = _ctx.tblDocumentImage.Where(DI => di.DocumentID ==的docID).FirstOrDefault();
            doc.File =(字节[])f.DocumentImage.ToArray();        返回文档;
    }
    公众的ActionResult下载(INT ID,字符串文件名)
    {
        文档DOC = NULL;        如果(ID!= 0)
        {
            文档= _fileRepo.DocumentById(ID);
        }        如果(文件!= NULL)
        {
            返回文件(doc.File,doc.ContentType,doc.FilenameWithExtension);
        }        返回文件(新的字节[0],text / html的);
    }


解决方案

尝试这样的:

  INT文件长度= Request.Files [I] .ContentLength;

而不是:

 长文件长度= Request.Files [I] .InputStream.Length;

One of our internal websites allows for users to upload documents and stores the files into the SQL 2008 database. The problem we are encountering is that docx are always saying the are corrupted when you try and open them. After you click OK, they then proceed to open fine. What I am doing wrong? (All other file types save fine, including .doc)

Upload code:

        // save the files
        string mimeType = Request.Files[i].ContentType;
        string filePath = Path.GetFileName(Request.Files[i].FileName);
        string fileName = Path.GetFileName(Request.Files[i].FileName);
        Stream fileStream = Request.Files[i].InputStream;
        long fileLength = Request.Files[i].InputStream.Length;

        if (fileLength > 0)
        {
            byte[] fileData = new byte[fileLength];
            fileStream.Read(fileData, 0, (int)fileLength);
            _fileRepo.SaveFileToDatabase(_currentUser.Username, t.EncounterId, fileName, filePath, mimeType, fileData);
        }

Code that inserts it into the database

        tblDocumentImage img = new tblDocumentImage();
        img.DocumentImage = file;       //DB field datatype = Image

...
...

        _ctx.tblDocumentImage.InsertOnSubmit(img);
        _ctx.SubmitChanges();

Code that serves the file back out

    public Document DocumentById(int docID)
    {
        Document doc = new Document(docID;
...


            var f = _ctx.tblDocumentImage.Where(di => di.DocumentID == docID).FirstOrDefault();
            doc.File = (byte[])f.DocumentImage.ToArray();

        return doc;
    }


    public ActionResult Download(int id, string filename)
    {
        Document doc = null;

        if (id != 0)
        {
            doc = _fileRepo.DocumentById(id);
        }

        if (doc != null)
        {
            return File(doc.File, doc.ContentType, doc.FilenameWithExtension); 
        }

        return File(new byte[0], "text/html");
    }

解决方案

Try like this:

int fileLength = Request.Files[i].ContentLength;

instead of:

long fileLength = Request.Files[i].InputStream.Length;

这篇关于上传的docx文件越来越损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆