开发者

how to link files out of website

开发者 https://www.devze.com 2023-03-06 12:16 出处:网络
Our application server has a folder used to store attachments uploaded by customer. This folder is called Attachments which is out of website, no virtual directory for this folder. I wonder how to lin

Our application server has a folder used to store attachments uploaded by customer. This folder is called Attachments which is out of website, no virtual directory for this folder. I wonder how to link files of this folder. My code is :

string vetting = "c:\\Attachments";
if (Directory.Exists(vetting +"\\"+ "UserName"))
        {

            DirectoryInfo di = new DirectoryInfo(vetting+"\\" + "UserName");
            FileInfo[] rgFiles = di.GetFiles("*.*");
            if (rgFiles.Length > 0)//thumb.db will be a file
            {//list is a div on front end.
                list.InnerHtml += <span class='SubTitle'>Your attachments list:</span>";
                foreach (FileInfo fi in rgFiles)
                {
                    list.InnerHtml += "<br><a href='c:/Attachments/UserName/"+fi.Name+"'>"+fi.Name+"</a>" ;
                }
            }
            else
            {
                list.InnerHtml += "You don't have any attachment yet.";
            }
        开发者_StackOverflow社区}

But it doesn't work, it always goes to find the client side c:/Attachments. How could I link to server side c:/Attachments? Thank you in advance.


You cannot link to those files if they're not exposed to IIS (your web server).

Suggest 2 options to consider:

  • create an .ashx (ASP.NET web handler)to handle these requests. It would read the requested document (either from session, cookie, or querystring) as you see fit. Your .InnerHtml can be <a href='mysite.com/bar.ashx?file=foo'>foo.xlsx</a>. You can then implement authorization checks, file name obfuscation, and a whole lot more. Read the bytes of the desired file, and return to the client.

  • create a virtual directory under your IIS site/application. Call this vdir 'attachments'. Warning: this lets anyone use/build/consume any link to view any attachment. Consider disabling directory browsing. Ensure that the appropriate IIS user has NTFS permissions to view this directory. Implement this if security isn't a concern/requirement. Your .InnerHtml can then be <a href='mysite.com/attachments/foo.xlsx'>foo.xlsx</a>


The C Drive is referencing the local machine, not a web server directory. Therefore, any user who clicks the link on your website will be directed to their own C drive. You will either need to upload your attachment folder to an existing directory on your web server, or create your own Virtual Directory.


Naturally it goes to the client side directory structure, that's where the browser is running. It has no knowledge of how the server is set up.

You need to expose those links via HTTP and so they need an address that resolves to an address that knows how to resolve those files on the server to resources available over HTTP.

0

精彩评论

暂无评论...
验证码 换一张
取 消