Check if files are present on same server asp.net - asp.net

I am working on Asp.net Website. I want to check if file are there on same server deployed application. How can i check that by physical path or virtual path ?
Eg: My website is deployed on Server A in directly Web1 which is under D drive.
I have one more website deployed on same server but different directory WEB2.There is folder in WEB2 directory say attachment ,which have all the media files eg pdf,doc etc.
So how can i check from Web1 that files are there in WEB2/Attachment folder ??

If they're on the same server, you can use File.Exists(file..)
If they're on different servers but still within the same network, map a drive or use UNC format when using File.Exists
If they're on different servers with no way of being able to see each other's drives, create a webservice within each site; add a method that takes a filename as a parameter, the webservice itself can do a File.Exists

Make sure that your application pool has access to the attachment folder. I think the app pool by default doesn't have access to a folder outside the web1 directory. Then you can execute the following code to get the list of files in the attachment folder.
string[] fullPathToFiles = Directory.GetFiles("D:\web2\attachment\")
or you can execute
File.Exists("D:\web2\attachment\<file.pdf>")
as sh1rts has suggested.

Related

How to deploy web application as a child/sub application in Azure? [duplicate]

I have an application running on Azure, what I need is to have the application copied so different users can access for example site.com/s1 and site.com/s2 and use them as separate. The application is the same, the database will change.
I tried to create a virtual directory for my app but I get the same error every time: "The physical path for virtual path '/s1' is invalid."
I tried creating a physical directory from FTP and I can see the new directory, but Azure don't recognize it as a physical one.
I would like to have wwwroot/s1 wwwroot/s2 and access those apps the way I explained before.
Please have a try to config the Virtual directory site/test1 on the Azure portal as following screenshot
When we push the WebApp to the Azure, we need to include the virtual directory path in the Site Name and Destination URL sections on the Connection tab
After that we can visit the WebApp with sitename/site1
We also can check the uploaded file from Azure Kudu tool (https://sitename.scm.azurewebsites.net/DebugConsole/?shell=powershell)

Accessing Files on Network from IIS

I have a shared path \\mynetworkshare\myfolder which has images stored.
I have a domain user mydomainuser which was granted with read access to files in that path
I hosted website on IIS. I created a Virtual Directory within my IIS Site with an Alias myphotos pointing to the Physical path \\mynetworkshare\myfolder. I have also clicked on Connect as... button in the Add Virtual Directory dialog box and provided my mydomainuser credentials.
I clicked on the newly created virtual director myphotos and click on Content View in the right pane. I'm able to view all my photos within IIS. From this, I assume the setup of virtual directory to my shared drive is correct.
Now, the question is how do I access this Virtual Directory or Files in it from my code?
I have tried below
var filePath = Server.MapPath("~/myphotos/" + "myimage.jpg");
When I write the filePath to a log file, I see it is trying to map to a physical folder setup within my website folders.
Instead of pointing to
\\mynetworkshare\myfolder\myimage.jpg
it is pointing to
d:\wwwroot\inetpub\mywebsitefolder\myphotos\myimage.jpg
I know Server.MapPath resolves to a physical path of hosted site but I wonder if it behaves the same with my virtual directory.
Or Do I need to let ASP.Net know somehow that myphotos is a virtual directory created on IIS? Or Am I on the wrong path to get files? Do I need to write code something different?
You can get the physical path from IIS (7+) using System.Web.Administration (available in NuGet)
var physicalPath = new Microsoft.Web.Administration.ServerManager()
.Sites["Default Web Site"]
.Applications["/MyApplication"]
.VirtualDirectories["/MyVirtualPath"]
.PhysicalPath;
If the virtual directory is in the root, the Application is "/"
What's specified in the Connect as... may only apply to direct web requests. To access the files from your application, you will need to configure the Application Pool to use mydomainuser as its Identity (found under Advanced Settings...), or you will need to grant share permissions to the computer running IIS if the Application pool uses a built-in account.
The step that was missing is converting Virtual Folder as Application.
Right-click the Virtual Folder myphotos, click on Convert to Application
The link here should explain the steps
Without adding Server.MapPath, it works fine. For example
image.src = "~/myphotos/myimage.jpg"

Questions about copy IIS config files to another server

I have several asp.net web services is running in IIS. Now I want to move to another server in all exactly same IIS setting and using same web services. I know about copying a IIS config files(ApplicationHost & administration) to new server remotely but do I also need to copy all asp.net files to new server as well? If yes then must the asp.net files be in the same file path directory as the current one(D:\Website) for IIS in new server to be able to recognize it?
Yes, you would need to copy the ASP.NET files as well.
It is not necessary to put them in the same file path directory. Whatever directory you put them in, ensure your IIS Website Settings point to that directory and you have setup the same set of permissions etc. on it.
e.g. You could have had a website on c:\site on one web server. you could move the website files onto another web server and put them on d:\newsite\ and change the Website Directory Path in IIS Manager to point to D:\newsite\ directory.

Access files/ folders remotely by authentication via browser/ web

We have some requirements where we want to allow our clients/ users to download files/ folders from our file server via browser/ web.
We have a many different directories created on our file server which is mapped to different clients. Which means that, every client has its own directory on our server. We have a main (root) directory for every single client/user. Which means that, every client’s files/folder are created under their respective main directory.
The only thing we need be sure is, whenever our client make a request to access/ download file, first we need to validate their credential (username/password are stored in our SQL server DB) and then we need to allow only those folder which is mapped for specific folder. (The folder mapping is again stored in our SQL server DB)
Which means that, after applying the credential by user/clients, they can only access their directory/ files. They cannot access other’s files/ directory.
Would anybody please do let me know how would I achieve this? All the suggestions would be appreciated highly.
Thanks in advance.
Since different users need to be authorized to access different directories, you cannot expose your files directly on the web.
Build a simple login with a custome MembershipProivder. That will allow you to autenticate against your database.
Since you know which directories the user has access to, you can fetch a listing of files and folders and present it in a ListView to the user.
You can write a HttpHandler to check user authorization and then serve the files to them.

How to retrieve the full path of file during file upload in ASP.NET

I am working on a WEB application where few clients have ability to upload the file. Actual files are mapped to server drive where few users would have access to.
Is it possible to get the full path of the file which is mapped to server?
HttpServerUtility.MapPath returns the physical file path that corresponds to the specified virtual path on the Web server.
http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath(v=vs.110).aspx

Resources