简体   繁体   中英

file.Exist returns true on localhost and returns false on server c#

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.

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. 在此处输入图像描述

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

在此处输入图像描述

在此处输入图像描述

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