Get the UNC of a file that the end user selects - c#

I am trying to create a Sharepoint webpart for a user to browse to, and select, a file from a share on our file server. Then I need to create a link to this file to display in a list of links that will go onto our Sharepoint intranet page. I have created a custom web part using asp.net/c# in order to do this but I am stuck on how to get the UNC path to the document. From my understanding it wont work with an asp.net fileupload control or a html input element. What other options would there be? I really don't want the user to have to type in the whole path to the document. This needs to be a re-usable solution so that my users can create new lists of links to documents when they so desire. Thanks for any advice.

I don't think the full file path is supported or allowed via modern web browsers via the file upload control. What you would end up having to do is create something server-side that use a service account to access the file share, and then the client (web page) could call the server-side code as it traverses the file share until the user selects a file.
Example:
server: on load, here are the contents of "\server\home"
client: show the contents of subfolder "\server\home\pictures"
server: connects to "\server\home\pictures" and returns contents
client: selects "\server\home\pictures\foo.jpg"
Check out System.IO.Directory http://msdn.microsoft.com/en-us/library/system.io.directory(v=vs.100).aspx for ways to get listing of directory contents server-side (GetFiles, GetDirectories, GetFileSystemEntries, etc) and then you can return those results to the client.

Related

Display pdf file from absolute path

I am using this code for creating pdf viewer in my application
https://amitpatriwala.wordpress.com/2009/08/28/pdf-viewer-in-asp-net/
it works fine when I give it a path for a file inside my application folders, ex: displaypdf1.FilePath= #"~/MyFolder/" + Hello.pdf;
but now I want to give this displaypdf1.FilePath an absolute path to read the pdf file which is not in my application folders, I tried but it didn't work!
A web page cannot access items that are outside of the website. If you want the web page to reference files located in D:\PDFs, for example, you need to create a virtual directory in your website that points to "D:\PDFs". Then the web pages can access them by ~/PDFs/Hello.pdf.
You'll also need to ensure that the website has appropriate permissions to access the directory.

how to create folder on server from code in c#?

Is there any way to show user popup to give the name to create folder
And with that if he does not want to create folder then he should be shown the list of folders to which he want to save the file
Is it proper way that I create a popup with javascript with the list of folders on my server and put a textbox and button to save the new and in code we will create directory in code passed with textbox?
string pathToCreate = "~/UserFolders/" + TextBox1.Text;
if(Directory.Exists(Server.MapPath(pathToCreate))
{
//In here, start looping and modify the path to create to add a number
//until you get the value needed
}
//Now you know it is ok, create it
Directory.CreateDirectory(Server.MapPath(pathToCreate));
string targetPath = Server.MapPath("FolderName"); //with complete path
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
fileToUpload.PostedFile.SaveAs(targetPath + ImageFileName);
}
else
{
fileToUpload.PostedFile.SaveAs(targetPath + ImageFileName);
}
As I mentioned by commenting that Functionality like SaveFileDialog is only possible in Windows Forms application, you cannot do this in asp.net
The alternatives in asp.net are difficult however as an alternate there is a third party editor available on the web called CKEditor 3.x Developer's Guide.
You can use File Browser (Uploader) for the purpose you want to achieve.
to create folder :
System.IO.Directory.CreateDirectory(newPath);//newpath is your folder creating path
If you are developing web applications (I can see you have tagged your question as asp.net), you do not need to worry about the pop up window. You just send the file through the Response and the download window will be shown by the browser.
Using the Example given in How to Create a Folder or File You can create a folder, but in asp.net you can not Use open file dialog or save file dialog functionalities, this will work only in C# Windows forms application.
Web applications and the HTTP protocol do not support direct manipulation of the server-side file system. In fact, that would be a dangerous idea.
It sounds like you have a base folder somewhere on the web server where you want users to be able to upload files, with some user control over subfolders and location of the uploaded file. I would suggest dong this by presenting the file system in a tree view or a list of links (where the links let you navigate up/down the folder tree). Combine this with a file input and you've got yourself a solution. HOWEVER, be very careful to control and cleanse the specified file name for the uploaded file. There are many combinations (such as utilizing "..") that can allow the user to hack into unwanted file locations on your server.

Accessing http://< someserver.com/logs:<someportnumber> > in client side using c# code

I have a scenario in my mind .. I need validations/suggestions from Stack over flow !! :)
There is a (remote)apache server hosting this URL "http://someserver.com/logs/log.txt:4041" .When i hit this URL in IE it opens a page containing log.txt in a file-folder-directory structure (after authentication).
Is there any way to get the attributes of the log.txt (attributes what i mean is file creation date,file modification date,file size etc..).
What I am planning to do is to write a code in C#.net(in the client) using system.IO namespace and using the fileinfo class and use
FileInfo fi = new FileInfo(pathname);
fi.CreationTime.toString() to retrieve the file creation time.
This is successful for files that exists in local directories in my hard drive!! .
Is it possible to use the same code for retrieving the information about the file that exists in the server that is accessed using the URL "http://someserver.com/logs/log.txt:4041" ?? if yes should i give the URL in my pathename ?
Take it for granted that i have access to the server by authentication..
You can try to inspect the data in WebResponse.Headers. The web server will send some date/time information of the file with the response. This may however not be what you expect depending on the web server you're calling and whether the page you are loading is a script or a file on disk. Settings of the web server will also influence the details returned.
You will not be able to use FileInfo method for items retrieved from http. However if the directory that the file that you are getting served to you is accessible from a share of from samba you could use this method.

Loading a pdf document using absolute path

I have a requirement where i want to load pdf document in a web page from a physical path. The pdf document location is not inside my website directory. To elaborate on this with example: Let assume my virtual directory refers to "c:\website". I have all my pdf documents stored under different folder called c:\pdfDocuments". On one of my web page i want to load my pdf document from c:\pdfdocuments. Is there way to pass the absolute path in this case (c:\pdfdocuments\x.pdf) to frame control's src attribute.
Thanks
CS
No, you cannot do that unless the C:\pdfdocuments is also a website; and in that case you would need to pass in the URL that relates to that physical path.
Keep in mind that the frame, or other html element, is trying to load the contents of the file accross the internet from the browser to your server. The browser on the clients end has no knowledge nor access to your physical filesystem, only what is exposed via the web server.
Now, if you're trying to load this on the server side, then you should be able to use the physical path as long as the worker process has access permissions to that path. But based on the question ".. to frame control's src attribute." I'm assuming you're referring the the client side html.

How to open a file stored in a server from aspx page using UNC share path?

I am listing the files stored in a remote server in the aspx page grid view with the file name linked (href) to the UNC path of the file.
when the user clicks the file name link, it should open the file from the server to allow the user read the file.
I am using impersonation (Using mirror logon method) for file upload and delete in the same page which works fine.
When I try to click the link to view the file it throws an error message box saying access denied.
While upoading / deleting file from my application, it uses the impersonated account I specified in web.config.
But it uses default ASP.NET (SERVER\IUSR_SERVER) account when trying to access via the link.
I cant give read access to all in my file server share.
How can I make the application use the impersonated account while trying to access the file for reading through the link I am showing for the files?
Or please suggest me an better alternative to do the same.
Thanks In advance.
When you generate the links in the Web page, do not point directly to the UNC, but to a page of your app, with a parameter to identify the file. In this page, use the same impersonation method to open the file and send its content back to the Web browser.

Categories

Resources