-1

I am working on a project in it I want to capture an image and save it to a server. When I run the application on localhost it works, but on the server it doesn't.

My code:

void CreatePhoto()
{
    string strPhoto = Request.Form["imageData"]; //Get the image from flash file
    byte[] photo = Convert.FromBase64String(strPhoto);

    FileStream fs = new FileStream(Server.MapPath("Images/Webcam.jpg"), FileMode.OpenOrCreate, FileAccess.Write);

    BinaryWriter br = new BinaryWriter(fs);
    br.Write(photo);
    br.Flush();
    br.Close();
    fs.Close();
}
Sam
  • 7,252
  • 16
  • 46
  • 65
SumitG
  • 41
  • 2
  • 8
  • //FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("Webcam.jpg"), FileMode.OpenOrCreate, FileAccess.Write); – SumitG Aug 20 '13 at 07:30
  • //FileStream fs = new FileStream(MapPath("192.168.21.4/d$/iis/VPMS/Images/Webcam.jpg"), FileMode.OpenOrCreate, FileAccess.Write); – SumitG Aug 20 '13 at 07:30
  • //FileStream fs = new FileStream(MapPath("C:\\Webcam.jpg"), FileMode.OpenOrCreate, FileAccess.Write); – SumitG Aug 20 '13 at 07:31
  • above all I try for FileStream – SumitG Aug 20 '13 at 07:31
  • Read that answer to see how to set the correct permissions to the file, just give him write permissions : http://stackoverflow.com/questions/16677887/how-to-set-correct-file-permissions-for-asp-net-on-iis/16678016#16678016 – Aristos Aug 20 '13 at 08:24

1 Answers1

-1

Probably web server does not have permissions to write in folder Images/webcam.jpg. I think that it will be better to store files in ~/App_Data folder

Novakov
  • 3,055
  • 1
  • 16
  • 32
  • 1
    -1 because if you save them on `App_Data` then the users can not see them ! This directory is protected. – Aristos Aug 20 '13 at 08:05
  • The question does not say that the file must be viewable directly. You always can create HTTP handler that serves that file – Novakov Aug 20 '13 at 08:14
  • If the issue have to do with the file permissions, then the issue will continue on the App_Data, plus you have the protection of this directory. The user must solve the file security issue, not to change directory. I will vote different if you remove the suggestion that is not correct and give instructions what permissions may the programmer have miss to give to that files. – Aristos Aug 20 '13 at 08:16
  • Yes, the issue is with file permissions. However I can imagine environment where web server can modify App_Data (e.g. it is popular to store database file there) and can not modify files outside it. Changing directory is simple solution that could work. – Novakov Aug 20 '13 at 08:20
  • I also try for FileStream fs = new FileStream(MapPath("App_Data /Webcam.jpg"), FileMode.OpenOrCreate, FileAccess.Write); but it not work – SumitG Aug 20 '13 at 08:37
  • So that issue with permissions. Are you deploying your application using Deployment Package or by copying your files? – Novakov Aug 20 '13 at 08:40
  • So you have to manually adjust permissions for correct folder. First determine what is the identity of app pool running your appliaction (you can check this in IIS Management Console by checking properties of AppPoll). If it is set to ApplicationPoolIdentity than you need to give permissions to IIS APPPOOL\NameOfAppPool, if it is Network Service/Local service/Local system that the user will be NetworkService/LocalServer/System. Needed permissions are: write and modify. – Novakov Aug 20 '13 at 08:52