1
我們的一個內部網站,允許用戶將文檔和存儲的文件上傳到SQL 2008數據庫。我們遇到的問題是,當您嘗試打開它們時,docx總是說它們已經損壞。點擊確定後,他們繼續打開罰款。我做錯了什麼? (所有其他類型的文件保存精美,包括.doc)上傳docx文件越來越損壞
上傳代碼:是將其插入數據庫提供文件退了出來
tblDocumentImage img = new tblDocumentImage();
img.DocumentImage = file; //DB field datatype = Image
...
...
_ctx.tblDocumentImage.InsertOnSubmit(img);
_ctx.SubmitChanges();
代碼
// 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);
}
代碼
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");
}