Problem when saving a file in a folder, which comes from type byte [] on the server when using WriteAllBytes in C #

0

I have the following function.

 private void guardarimagen( byte[] img)
 {
                var path = Server.MapPath("~/Images/prueba.jpg");
                System.IO.File.WriteAllBytes(path, img);
  }

EN localhost works fine, but when I try it in production, it tells me error: [object Object]

I imagine it has to do with the issue of permissions, but there must be another way to implement by code to make it work.

If anyone has any ideas, I would appreciate it a lot.

    
asked by Danilo 11.10.2018 в 19:06
source

1 answer

1

I have this function .. but it works with a base64. to convert from bit array to base64 you just have to do this:

byte[] bytes = File.ReadAllBytes(direccion);
string image = Convert.ToBase64String(bytes);





//dale un base64 de una imagen y te la convertira a un archivo fisico
//si el directorio que le pasaste no existe.. lo creara
public static string ToImage(string base64coded, string sName, string sRuta)
{
    try
    {
        string folderRoute = sRuta;
        if (!Directory.Exists(folderRoute))
        {
            Directory.CreateDirectory(folderRoute);
        }
        string imageName = sName + ".jpg";
        if (base64coded.IndexOf("data:image/jpeg;base64,") == 0)
        {
            base64coded = base64coded.Replace("data:image/jpeg;base64,", "");
        }
        var bytesss = Convert.FromBase64String(base64coded);
        string Route = folderRoute + "\" + imageName;
        using (var imageFile = new FileStream(Route, FileMode.Create))
        {
            imageFile.Write(bytesss, 0, bytesss.Length);
            imageFile.Flush();
        }

        return Route;
    }
    catch (Exception ex)
    {

        throw ex;
    }
}
    
answered by 11.10.2018 в 19:44