Skip to content

Instantly share code, notes, and snippets.

@iformas
Created October 29, 2018 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iformas/73ed17956e6dd6e5cd80133d5cbbfe35 to your computer and use it in GitHub Desktop.
Save iformas/73ed17956e6dd6e5cd80133d5cbbfe35 to your computer and use it in GitHub Desktop.
example upload and list
[CustomHandleError]
[Authorize]
[HttpPost]
public ActionResult AddArchivosPorPieza(FormCollection collection)
{
try
{
long piezaId;
String observacion = collection["observacion"];
if (collection["piezaId"] != null && Int64.TryParse(collection["piezaId"], out piezaId))
{
var pieza = ServiceFactory.Instance.Pieza().GetPiezaById(piezaId);
if (pieza != null && !pieza.eliminado && pieza.nombreComunId != null)
{
List<String> imagenes = new List<string>() { "image/bmp", "image/jpg", "image/jpeg", "image/png" };
List<ArchivoPorPieza> archivosPorPieza = new List<ArchivoPorPieza>();
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFileBase file = Request.Files[i];
if (file != null && file.ContentLength > 0)
{
file.InputStream.Position = 0;
MemoryStream fileData = new MemoryStream();
file.InputStream.CopyTo(fileData);
byte[] data = fileData.ToArray();
if (data.Length > 0)
{
archivosPorPieza.Add(ServiceFactory.Instance.Pieza().AddArchivoPorPieza(new ArchivoPorPieza()
{
metadataArchivoId = ServiceFactory.Instance.MetadataArchivo().AddMetadataArchivo(new MetadataArchivo()
{
nombre = file.FileName,
mime = file.ContentType,
Archivo = new Archivo() { data = data, archivoGUID = Guid.NewGuid() }
}).metadataArchivoId,
piezaId = piezaId,
observacion = observacion,
metadataArchivoThumbnailId = imagenes.All(x => x != file.ContentType) ? (long?)null : ServiceFactory.Instance.MetadataArchivo().AddMetadataArchivo(new MetadataArchivo()
{
nombre = file.FileName,
mime = file.ContentType,
Archivo = new Archivo() { data = ImageTransformer.Resize(fileData, 80, new Size(150, 0)), archivoGUID = Guid.NewGuid() }
}).metadataArchivoId
}
));
}
}
}
return Json(new { isok = true, data = archivosPorPieza }, JsonRequestBehavior.AllowGet);
}
return Json(new { isok = false, error = "La pieza ingresada no es valida" }, JsonRequestBehavior.AllowGet);
}
return Json(new { isok = false, error = "El archivo ingresado no es valido" }, JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
Log.Error("Error " + e);
return Json(new { error = "Ocurrio un error, por favor intente nuevamente" }, JsonRequestBehavior.AllowGet);
}
}
[CustomHandleError]
[Authorize]
public async Task<ActionResult> GetArchivoPorPieza(long id, bool returnFile = true, bool full = true)
{
try
{
ArchivoPorPieza archivoPorPieza = ServiceFactory.Instance.Pieza().GetArchivoPorPieza(id, returnFile, full);
if (archivoPorPieza != null)
{
if (returnFile)
{
var cd = new System.Net.Mime.ContentDisposition
{
FileName = full ? archivoPorPieza.MetadataArchivo.nombre : archivoPorPieza.MetadataArchivo1.nombre,
// always prompt the user for downloading, set to true if you want
// the browser to try to show the file inline
Inline = true,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
if (full) return await Task.Run(() => File(archivoPorPieza.MetadataArchivo.Archivo.data, archivoPorPieza.MetadataArchivo.mime));
return await Task.Run(() => File(archivoPorPieza.MetadataArchivo1.Archivo.data, archivoPorPieza.MetadataArchivo1.mime));
//return File(archivoPorPieza.MetadataArchivo.Archivo.data,archivoPorPieza.MetadataArchivo.mime);
}
return Json(new { isok = true, data = ServiceFactory.Instance.Pieza().GetArchivoPorPieza(id, false) }, JsonRequestBehavior.AllowGet);
}
return Json(new { isok = false, message = "El archivo no Existe" }, JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
Log.Error("Error " + e);
return Json(new { isok = false, message = "Ocurrio un error" }, JsonRequestBehavior.AllowGet);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment