Displaying files in a directory - c#

I am developing an ASP.NET 3.5 web application and I have a folder in the project which contains a list of documents which could be pdf or any of the MS office 2003 or 2007 supported file formats. I would like to display these files to my users as thumbnails (just like the way windows displays files). And when the user clicks on a file it has to prompt them to either save the file or open in the browser itself. How can I achieve this?

You can get the files like this (assuming /Documents)
string path = Server.MapPath(#"/Documents");
string[] files = System.IO.Directory.GetFiles(path);
And nou you only have to write some HTML generating code to display the files the way you want them displayed.

the answer at the following question seems to be RIGHT up your alley (ie the answer)
C# get thumbnail from file via windows api

Related

Upload video to SharePoint library by code

I am making a app for SharePoint and have a form with several inputs for text and for files as well. The input for files works fine for regular files such as Word, Excel, PowerPoint, images etc... but wont work with videos. Is it possible to upload a video to a document library by code? Or cant you do that simply because the files is usually to big and would take long to upload?
The files is of the type HttpPostedFileBase and sent by a normal mvc form.
And this is the code for uploading it to the folder
FileCreationInformation attachFileInfo = new FileCreationInformation();
attachFileInfo.ContentStream = pNewRequest.FileToUpload1.InputStream;
attachFileInfo.Url = Path.GetFileName(pNewRequest.FileToUpload1.FileName);
newFolder.Files.Add(attachFileInfo);
According to this StackExchange answer, SP videos are organized in special VideoSet folders which seem to be similar to doc sets.
You said you're using MVC to submit the files so I'm assuming you're using CSOM on an external server. Here's an example for handling video uploads via CSOM.

Interact with aspx page to download a file from C#

I'm attempting to automate a task I have of interacting with a website form to download a file.
In human terms, I select a label from a list of options, and then press a submit button. The website then automatically downloads a zip file to the downloads folder.
The actions described above have the net effect of running this code in a browser console:
$('#ListBox1').val($('#ListBox1 option:first')) && $('#Download_0').click()
My question is, how can I download this zip file by running the code from a C# program? Ideally, I would also be able to specify where the file downloads to, although I'm not sure if this is possible.
Please note, this question is not asking how to download a file using C# (see: How to download a file from a website in C# and .net - Downloading a file from a website using C#). No such file exists on the website for me to download. The resulting file is generated and downloaded automatically by the two actions above.
EDIT:
The following code does download a file, however, the file is 24kb (the zip file I'm expecting is around 8mb) and unopenable:
using (WebClient client = new WebClient())
{
client.DownloadFile("https://cdr.ffiec.gov/public/PWS/DownloadBulkData.aspx", AppDomain.CurrentDomain.BaseDirectory + "download.zip");
}
EDIT2:
What I'm attempting to do is download the most recent zip file available containing bulk call report data. The website selects the most recently available data automatically, but I have to manually select the type of file I want to download. As such, the web request won't be static and can't be hard-coded into the program, which is why I'm attempting to interact with the webpage.

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.

uploading a file from folder with out browse in asp.net

i just wanna upload afile from a fixed path, so i dont want browse button, I need just a TextBox(Path of my file some thing like c:/junk/upload) and and upload button.some thing like
[TextBox.Path][UploadButton]
or can i get the code in java applets or any other lanaguage?
For security reasons, a webpage loaded in a browser from internet cannot directly refer the local file system. You need a desktop application or plugin to achieve this.
no we can make using asp.net or java applets and yes what you said is right, security reasons! , but every applications has a sand box limits and thats include file handling as default , and we achived that. with out problem

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.

Categories

Resources