Я использую метод из первого ответа в этом сообщении: How to create byte array from HttpPostedFile, но по некоторым причинам он не работает на .docx-файлы.Как получить массив байтов из HttpInputStream для файла docx?
//viewmodel.File is HttpPostedFileBase
byte[] fileData;
using (var binaryReader = new BinaryReader(viewModel.File.InputStream))
{
fileData = binaryReader.ReadBytes(viewModel.File.ContentLength);
}
На .docx файлов fileData
показывают как {byte[0]}
, но она работает с PDF-файлами, файлы Excel (XLSX), предварительно 2007 файлов Word (DOC), и изображений (т.е. значения больше нуля). Сохраненный в базе данных файлData - 0x
.
Как получить массив байтов из HttpInputStream для файла docx?
UPDATE
Мой web.config настроен
<httpRuntime targetFramework="4.5" maxRequestLength="102400" />
Это работает с XSLX файлы размером более 4 Мб, но DOCX файлов меньше, чем 80KB нет.
UPDATE 2
я могу получить FILEDATA заселить, используя метод, описанный здесь: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfile.aspx
byte[] fileData = new byte[viewModel.File.ContentLength];
viewModel.File.InputStream.Read(fileData, 0, viewModel.File.ContentLength);
Но если я сохранил этот массив байтов в базе данных и попытаться записать файл, это сильно коррумпированы. Добавлено в базу данных в этом случае выглядит как 0x00000000000000000000000...
UPDATE 3
Вот весь метод управления, хотя я не думаю, видя все это необходимо:
[HttpPost]
public ActionResult SaveChangeFile(AttachmentFormViewModel viewModel)
{
if (viewModel.File == null)
return Json(new { success = false, message = "No file was found, please select a file and try again." }, "text/x-json",
JsonRequestBehavior.DenyGet);
try
{
//Validate that the right kind of File has been uploaded
OperationResponse response = _attachmentProcessor.ValidateAttachmentContentType(viewModel.File, (ChangeFileTypeEnum)viewModel.FileType);
if (!response.IsSuccess)
return Json(new { success = response.IsSuccess, message = response.Message }, "text/x-json", JsonRequestBehavior.DenyGet);
UpdateProjectFromCostCalculatorRequest projectValues = null;
Workbook workbook = null;
Document document = null;
if (_attachmentProcessor.IsWorkbook(viewModel.File))
workbook = new Workbook(viewModel.File.InputStream);
if (_attachmentProcessor.IsDocument(viewModel.File))
document = new Document(viewModel.File.InputStream);
var filename = Path.GetFileName(viewModel.File.FileName);
//if cost calc, validate that the values are correct and update related project
switch ((ChangeFileTypeEnum)viewModel.FileType)
{
case ChangeFileTypeEnum.CostCalculator:
response = _attachmentProcessor.ValidateCostCalculator(workbook, filename);
if (response.IsSuccess)
projectValues = _attachmentProcessor.GetDataFromCostCalculator(workbook);
break;
case ChangeFileTypeEnum.DataValidation:
response = _attachmentProcessor.ValidateDataValidation(workbook);
break;
case ChangeFileTypeEnum.WorkPaper:
response = _attachmentProcessor.ValidateWorkPaper(document);
break;
}
//return error message if any of the validations above failed
if (!response.IsSuccess)
return Json(new { success = response.IsSuccess, message = response.Message }, "text/x-json", JsonRequestBehavior.DenyGet);
//get the file from the stream and put into a byte[] for saving the database
byte[] fileData;
using (var binaryReader = new BinaryReader(viewModel.File.InputStream))
{
fileData = binaryReader.ReadBytes(viewModel.File.ContentLength);
}
var file = new ChangeFile
{
ChangeRequestID = viewModel.ChangeRequestId,
ChangeFileTypeID = viewModel.FileType,
File = fileData,
Filename = filename,
ContentType = viewModel.File.ContentType,
CreatedBy = User.UserNameWithoutDomain(),
UpdatedBy = User.UserNameWithoutDomain(),
CreatedDate = DateTime.Now,
UpdatedDate = DateTime.Now
};
_changeRequestService.SaveChangeFile(file);
var log = new ChangeFileImportLog { CreatedDate = DateTime.Now };
switch ((ChangeFileTypeEnum)viewModel.FileType)
{
case ChangeFileTypeEnum.CostCalculator:
var project = _changeRequestService.GetChangeProjectByPsrs(file.ChangeRequestID, projectValues.PsrsNumber);
if (project != null)
{
_attachmentProcessor.UpdateChangeProjectWithProjectValues(project, projectValues);
log.NumberOfErrors = 0;
log.NumberOfSegmentChanges = 0;
log.NumberOfWarnings = 0;
}
else
{
log.NumberOfWarnings = 1;
log.Warnings =
String.Format(
"There is no project on this Change Request with PSRS \"{0}\". If there was, the new cost would be updated with \"{1:C0}\"",
projectValues.PsrsNumber, projectValues.Cost);
}
break;
case ChangeFileTypeEnum.DataValidation:
log = _attachmentProcessor.CreateChangeSegmentsFromDataValidation(workbook, file.ChangeRequestID, file.ChangeFileID, User);
break;
case ChangeFileTypeEnum.WorkPaper:
log = _attachmentProcessor.UpdateChangeProjectsFromWorkPaper(document, file.ChangeRequestID, file.ChangeFileID,
User);
break;
}
log.CreatedBy = User.UserNameWithoutDomain();
log.CreatedDate = DateTime.Now;
log.UpdatedBy = User.UserNameWithoutDomain();
log.UpdatedDate = DateTime.Now;
_changeRequestService.SaveChangeFileImportLog(log, file.ChangeFileID);
_changeRequestService.Commit();
return Json(new { success = response.IsSuccess, message = response.Message }, "text/x-json", JsonRequestBehavior.DenyGet);
}
catch (Exception ex)
{
return Json(new { success = false, message = String.Format("A system error was encountered: {0}", ex) }, "text/x-json", JsonRequestBehavior.DenyGet);
}
}
Файл docx не должен обрабатываться иначе, чем любой другой тип файла. –
Нет причин для этого. Что значит «пустой»? is filedata null? и никаких исключений не бросали? – Vivek
Вероятно, файл больше допустимого размера загрузки, если другие файлы работают. См. Это сообщение, http://stackoverflow.com/questions/288612/how-to-increase-the-max-upload-file-size-in-asp-net, чтобы настроить этот размер файла. –