简体   繁体   中英

Save an image on asp.net server

I'm trying to save a piture recovered from a byte[] on the asp.net server. Here is my code :

MemoryStream ms = new MemoryStream(cli.LOGO, 0, cli.LOGO.Length);

            ms.Write(cli.LOGO, 0, cli.LOGO.Length);

            string thePath = Server.MapPath("~/App_Data");
            string wholePath = thePath + "\\logo.jpg";
            FileStream fs = new FileStream(wholePath, FileMode.Create);

            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(cli.LOGO);
            bw.Close();

Where cli.LOGO is a byte array. I'm just trying to save it as a .jpg image in my App_Data folder (or anything else of course) but... It's doing nothing. cli.LOGO is not empty, but the file is not created... Why so ? Is it the proper way to save an image ? Thanks !

Try like this which is a bit shorter than your code:

string path = Server.MapPath("~/App_Data/logo.jpg");
File.WriteAllBytes("file", cli.LOGO);

try doing this

 if (Request.Files["Photo"] != null)
            {
                string path = "/uploads/" + DateTime.Now.Ticks.ToString() + "_" + Request.Files["Photo"].FileName;
                Request.Files["Photo"].SaveAs(Server.MapPath(path));
                SP.PhotoPath = path;

                //The MapPath method maps the specified relative or virtual path to the 
                //corresponding physical directory on the server.
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM