Tengo algunos base64 almacenados en una base de datos (que en realidad son imágenes) que deben cargarse a un tercero. Me gustaría cargarlos usando la memoria en lugar de guardarlos como una imagen y luego publicarlos en un servidor. ¿Alguien aquí sabe cómo convertir base64 a una secuencia?
¿Cómo puedo cambiar este código:
var fileInfo = new FileInfo(fullFilePath); var fileContent = new StreamContent(fileInfo.OpenRead());
para llenar el objeto StreamContent con una interpretación base64 de un archivo de imagen.
private static StreamContent FileMultiPartBody(string fullFilePath) { var fileInfo = new FileInfo(fullFilePath); var fileContent = new StreamContent(fileInfo.OpenRead()); // Manually wrap the string values in escaped quotes. fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { FileName = string.Format("\"{0}\"", fileInfo.Name), Name = "\"name\"", }; fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); return fileContent; }
Querrá hacer algo como esto, una vez que haya obtenido la cadena de la base de datos:
var bytes = Convert.FromBase64String(base64encodedstring); var contents = new StreamContent(new MemoryStream(bytes)); // Whatever else needs to be done here.
Solo como un enfoque alternativo, que funciona bien con flujos grandes (guarda la matriz de bytes intermedia):
// using System.Security.Cryptography // and assumes the input stream is b64Stream var stream = new CryptoStream(b64Stream, new FromBase64Transform(), CryptoStreamMode.Read); return new StreamContent(stream);