Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

286
Views
Algunos tipos de archivos adjuntos en PDF guardados como flujo de octetos

En mi aplicación web asp.net MVC, hay algunas vistas que pueden cargar archivos PDF y archivos de imagen.

Al recuperar los mismos datos de la base de datos, el usuario puede ver los archivos adjuntos.

Pero noté algunos archivos PDF, el usuario seleccionó y cargó los archivos que se guardan en la base de datos.

Al recibir el archivo, dice que el archivo está dañado o no se puede abrir. Así que revisé el tipo de archivo de la base de datos, y se guardó como octet-stream y el archivo adjunto no se guardó en la tabla. ¿Alguien puede decirme por qué sucede esto y cómo evitar que esto suceda?

Estos son los códigos que estoy usando actualmente.

jQuery para la validación de archivos

 < script type = "text/javascript" > function checkImage(obj) { var fileExtension = ['jpeg', 'jpg', 'png', 'gif', 'bmp', 'pdf']; var ResponceImgId = $(obj).data('id'); const fileSize = obj.files[0].size / 1024 / 1024; // in MiB const fileSizeRule = 3.0; if ($.inArray($(obj).val().split('.').pop().toLowerCase(), fileExtension) == -1) { alert('error', 'Upload Error', 'Only .jpeg, .jpg, .png, .gif, .bmp ,pdf formats are allowed.'); } else if (fileSize > fileSizeRule) { alert("File size should be less than 3 MB. Please add a attachment less than 3 MB"); } else { var files = obj.files; var reader = new FileReader(); name = obj.value; reader.onload = function (e) { $('#' + ResponceImgId).prop('src', e.target.result); }; reader.readAsDataURL(files[0]); } } </script>

Convertir el archivo a byte

 //region converting attachments to byte public byte[] ConvertToBytes(HttpPostedFileBase Attachment) { byte[] imageBytes = null; BinaryReader reader = new BinaryReader(Attachment.InputStream); imageBytes = reader.ReadBytes((int) Attachment.ContentLength); return imageBytes; }

Esta es la forma de recuperar el archivo.

 public ActionResult RetrieveImage(int id) { var q = from temp in db.PurchasingItems where temp.Id == id select temp.Attachment; var type = from t in db.PurchasingItems where t.Id == id select t.FileType; string fileType = type.First().ToString(); byte[] cover = q.First(); if (cover != null) { if (fileType == "pdf") { return File(cover, "pdf"); } else { return File(cover, "image/jpg"); } } else { return null; } }

Imagen

ingrese la descripción de la imagen aquí

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!