-1

I am using the below code to save an pdf file in the particular folder. if the folder is not there it will auto create an folder. It is working fine in local. But when i publish the website to IIS. it doesnt work.

  string folderPath = "D:\\Labels\\";
                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }
                string file = @"D:\Labels\" + DateTime.Today.ToString("dd-MM-yyhh-mm-ss") + ".pdf" + "";
                var fileStream = File.Create(file);
                // change file name for PNG images
                responseStream.CopyTo(fileStream);
                responseStream.Close();
                fileStream.Close();
                ShowMessage("Downloaded Sucessfully", MessageType.Success);
  • 1
    Make sure the directory has enough permission for IIS User. Also check The Drive ,ie "D" in web server. – Biju Kalanjoor Jun 11 '21 at 07:34
  • 1
    This code is nunning on the server side. Does your server have a drive d? – derpirscher Jun 11 '21 at 07:35
  • i want to save the file in the local who ever use the website – Vigna Hari Karthik Jun 11 '21 at 07:42
  • 1
    @VignaHariKarthik That's not possible. Your code is running on the server side, and can only access serverside resources. It's also not possible in javascript on the client side because that would be a huge security issue if a website could access the local harddrive. Besides that, imagine what happens if a user with MacOS, Linux or a smartphone accesses your site. What would these systems do with a path like `D:\...` ? Even most windows machines nowadays don't have a `D:` drive anymore ... – derpirscher Jun 11 '21 at 14:24
  • 1
    With your local deployment it seemed to work. But only because both, server and client where running on the same machine – derpirscher Jun 11 '21 at 14:26

1 Answers1

1

Ι want to save the file in the local who ever use the website

This is not possible - what you can offer is a link to download the file - you can not run the File. functions on client from server.

The File. functions can work only for server side, and only if you have permissions on the directories.

How to set correct file permissions for ASP.NET on IIS
file download by calling .ashx page
What is the best way to download file from server

Aristos
  • 66,005
  • 16
  • 114
  • 150