Downloading files - asp.net

I am using asp.net/C#. I have a url to a folder on a remote server. In that folder are images that are all .jpgs. I don't know the names of the files, just that they are all .jpgs. How do I download all the .jpg images using asp.net/C# to a folder on my local hard drive.
Just to clarify. I am pulling files from a remote server and I am saving them to my local machine. I was given a web URL and told that the files I needed to pull down every night where .jpg image files. That's all I was told. I have no idea how I can get a list of files on a remote server with just the url to the folder.
Thanks

If it's a web URL, you'd have to depend on the web server giving you some sort of list of files. The format of the list could be almost anything.
Put it this way: using a browser or anything else, how would you as a human find out all the filenames?
Just to clarify, are you writing code on the server which has the files? If so, you can find out what files are present using Directory.GetFiles. What do you want the user to have to do at the local side?
If you could make your question a bit clearer it would really help.

Here is some concept code to work with
DirectoryInfo di = new DirectoryInfo("M:\MappedDrive");
FileInfo[] rgFiles = di.GetFiles("*.aspx");
foreach(FileInfo fi in rgFiles)
{
Response.Write("<br>" + fi.Name + "");
}

Related

Rooted Path and FileUpload Control

I know it's been asked and I have read the posts and Googled this all day. Still nowhere near something that works. Using an .aspx page, I need to upload a .pdf file to a specific website. I'm doing development using VS2017 and VB.Net. The app will run on different websites. It needs to upload client files to a specific different website and path. Also, the file name of the uploaded file will not be the same as the local source file. Creating the new name is no problem.
Let's say a local file must be uploaded to a website at https://www.appfileserver.co.za/pdfdocs, but I'm on https://www.myownsite.com. So, when using FileUpload1.SaveAs(rootedpath) the path that goes in there must be the rooted path to the target. What would the rooted path look like for the example I provided?
FYI, I know the IP addresses, http paths and anything else I need to know because I control those sites. It would be great to do an FTP upload. I have done this many times from desktop apps. Unfortunately I'd need the full path to the local file. It seems there is no way a web page is allowed to get that full path, so FTP upload is out - or is there a way?
After battling for two days trying to FTP upload from website to website (which is not possible because server firewalls block this), I finally solved it. The solution was a simple one. I deployed the upload .aspx file on the target server then embedded that in an iframe on the client machine apps. The files are then uploaded one time to the right place. Simple and 100% effective. Hopefully somebody see's this and understands it - so as to avoid the troubles I had.

Server.MapPath does not find the path on Azure

I have deployed my project to Azure. In my project I have "App_Data\Images" folder.
Now I'm trying to do the following:
String filename = GLOBAL_IMAGES_VALS.GET_FILE_PREFIX(imageType) + "-" + User.Identity.GetUserId<int>().ToString() + Path.GetExtension(image.FileName);
String origPath = Server.MapPath("~\\App_Data")+"\\Images\\" + filename;
But then upon trying:
image.SaveAs(origPath);
I get this error message:
Could not find a part of the path
'D:\home\site\wwwroot\App_Data\Images\logo-10003065.jpg'.
How can I save my file to "App_Data\Images\"?
The actual problem was that the sub-folder 'Images' did not exist. I can't remember why the publish process did not create this sub-folder, however I added it manually and then everything worked fine.
EDIT:
As others wrote here (#Spectarion). I'll put here the important remark that explain why the folder was not created:
Just for the future readers, folder won't be created if it's empty.
Folder is empty even if there are files that are not included in
project.
Just put some 'fake.txt' file into any folder you want to make sure that it will be created, and of course don't forget to add it to the project. Good luck.
Since you don't have any file in the particular folder, while publishing Web deploy ignores the empty folder.
Quick fix: Add any file to the folder before publishing will fix this issue.
if (!Directory.Exists(Server.MapPath("~/Images")))
{
Directory.CreateDirectory(Server.MapPath("~/Images"));
}
The directory might be missing in the folder. Create the directory and use it in file path
Maybe this :
System.Web.Hosting.HostingEnvironment.MapPath("~\\App_Data")+"\\Images\\" + filename )
Maybe the images folder doesn't exist and you need to create it first? Although I wouldn't recommend saving images in your app like this if it is designed for people uploading images. I would save them in Azure storage via blobs or the new Azure File storage. I would keep your app deployment files clean just related to your app and save any user generated content outside of it.
BTW, If you are using Azure Web Apps you can use the environment variable of "HOME" to always get the correct path (which should be D:\home)
string path = Environment.GetEnvironmentVariable("HOME") +
"\\site\\wwwroot\\App_Data\\images"
I assume your AppData folder is just under the wwwroot folder, which is usually the case.
Try this:
HttpContext.Current.Server.MapPath(Path.Combine("~/AppData/Images/", filename));
I just had this problem on VS15. I first followed the advice in this question in order to generate the error you've got. I'm guessing this follows in part dsb's answer, but dsb hasnt given any description of the actual process of fixing this.
I then went to https://<mywebsite>.scm.azurewebsites.net/DebugConsole to look through the directory and found that App_Data had not been published
Which was why the error was throwing. So, I then solved this by simply going to the solution explorer, right clicking App_Dataand selecting to "Publish App_Data".
However, my website was a short-term academic effort for a project - I think there is probably a lot to be said for considering Matt Watsons answer above about whether or not allowing users to upload to the deployment area is a good idea

Where should I save files dynamically generated by the ASP.NET Web API?

My ASP.NET Web API service dynamically generates files for users to download.
The remote application sends a request to the API which:
Generates the file and saves it ----> where?? <-----
Returns the URL of the file location so it can be downloaded.
Nothing fancy there, but my question is what are the best practices for where to save these files?
Some digging around suggests that App_Data might be the appropriate place but I haven't seen anything definitive.
Some considerations:
This all happens on a company intranet so security isn't really a big deal.
The files are essentially temporary. Is there a simple way to delete the file once it's downloaded? Not a big deal - I can set up a scheduled task to clean out whatever the destination folder is.
Thanks!
You can write to any location your application stores data in.
I recommend using the system Temp directory, that's what it's there for (System.IO.Path.GetTempPath()) or if your have write access, a subdirectory of your application:
String path = Server.MapPath("~/Temp");
if( !Directory.Exists( path ) ) Directory.CreateDirectory( path );
using(FileStream fs = File.OpenWrite( Path.Combine( path, "TempFileName.dat" ) )) {
fs.Write( new Byte[1024] );
}
If you have access to your web server, you could also create a virtual directory under your web application and configure it to point to whatever file location you want. Then you can have your application save the files into that virtual directory using System.IO. That's what I've done in the past.

no acces to .txt with Webmatrix ASP.NET

I got a tiny website for my company to check in which store a brand can be found.
The website uses a c# code to check .txt files in a subfolder.
It worked nice while I used a path like "C:\brands" on my pc but now I try to host the homepage so I changed the path to "~/App_Data/Brands/Normal" + StoreNames()[i] + ".txt".
But now I can't access the .txt files anymore.
The txt files are located in root/App_Data/Brands/Normal but the code is in root/App_Code. Could that be a problem?
Also I'm trying to access the files without any special permissions or an account.
The host is https://panel.sitecloud.cytanium.com/.
What do I need to get access to the files again?
It could be a problem with my code too because it doesn't work on localhost either (where it worked with an extern folder)..
Edit:
Okay, MapPath did the job! I just forgot to add MapPath also to
if(File.Exists(HttpContext.Current.Server.MapPath(sFilename)))..
Try to use HttpContext.Current.Server.MapPath to get the physical directory:
string data = File.ReadAllText(HttpContext.Current.Server.MapPath("~/App_Data/TextFile.txt"));

How to download a file from a UNC mapped share via IIS and ASP

I am writing an ASP application that will serve files to clients through the browser. The files are located on a file server that is available from the machine IIS is running on via a UNC path (\server\some\path).
I want to use something like the code below to serve the file. Serving files that are local to the machine IIS is running on is working well with this method, my trouble is being able to serve files from the UNC mapped share:
//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";
//Get the physical path to the file.
string FilePath = MapPath("acrobat.pdf");
//Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath);
Response.End();
My question is how I can specify a UNC path for the file name. Also, to access the file share I need to connect with a specific username/password.
I would appreciate some pointers on how I can achieve this (either using the approach above or by other means).
I'm not an ASP guy so I might be completely wrong with these answers.
Regarding the path, I don't think you should be using MapPath, since that's to get a relative path and you already know the physical path so can't you just change that to:
string FilePath = #"\\Server\Directory\FileName.txt";
Regarding the account, I think you need to use impersonation, this link seems to discuss just this:
http://aspalliance.com/336_Upload_Files_Using_ASPNET_Impersonation_and_UNC_Share.all

Resources