-2

I've a grid that contains file list (that on another server) and download buttons for them on each row. When button clicked, file should be downloaded. When I click it on localhost, file.Exist returns true and I can download the file. However, when I try same button on server (IIS), file.Exist returns false and the file cannot be downloaded. (This server also can reach the file.) My code snippet is below;

            var fileNameToShow = "8D.xls";
            var fileNameAndPath = "\\\\10.1.101.151\\Files\\Live\\10\\8D.xls"
            FileInfo file = new FileInfo(fileNameAndPath);
            file.Refresh();
            if (file.Exists)
            {
                // Send the file to the browser
                Response.Clear();
                Response.AddHeader("Content-Disposition",
                    "attachment; filename= " + fileNameToShow + "; size=" + file.Length.ToString());
                Response.TransmitFile(fileNameAndPath);
                Response.Flush();
                Response.End();
            }
            else
            {
                throw new Exception("File does not exist!");
            }

What can I do to solve this?

Edit: File is in 10.1.101.151. IIS is in another server. My local is also another PC.

1teamsah
  • 1,863
  • 3
  • 23
  • 43
  • That simply means you forget to learn the key differences, https://blog.lextudio.com/web-application-differences-in-visual-studio-and-iis-60fec7e311b3 – Lex Li Nov 26 '20 at 06:38
  • You can access the file on localhost because the localhost is running with you credentials and has permission to read from the remote server. On a IIS the server is not running with your credentials. You have to setup the roles on the IIS to run with your credentials. See : https://www.guru99.com/deploying-website-iis.html – jdweng Nov 26 '20 at 07:40

1 Answers1

0

First, you need to set the permissions of the folder on the server, and add the server where the IIS is located to the server where the 8D.xls is located. enter image description here

Second, you need to set the identity of the application pool, select a custom account, you can set the administrator.

enter image description here

enter image description here

Bruce Zhang
  • 2,880
  • 1
  • 5
  • 11
  • I edited the question. Could you please consider it again? – 1teamsah Nov 27 '20 at 06:39
  • While I reproduced the issue and tested it, I put file on server1 and IIS on server2. I access the site on my local machine. The code I use in the application is the same as yours, the only difference is the file path, \\\\server IP\\c\\Files\\Live\\10\\8D.xls. I store it in the C drive of the server1, so there's a C in the path. – Bruce Zhang Nov 30 '20 at 08:37