Getting file InputStream from http - http

I want to get some files from my ftp hosting, its a GoDaddy hosting services.
If I try to download a .txt file I get the Input stream no problems, but if I try to download my .pom file, I get a FileNotFoundException.
A piece of code:
storage=(HttpURLConnection) new URL(pathToFile).openConnection();
storage.setDoInput(true);
storage.setUseCaches(false);
storage.setRequestProperty("Accept","*/*");
return storage.getInputStream();
Any ideas?
Adam.

Some webservers, such as IIS6 and later, won't serve files if they don't know their MIME type.

Related

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"));

Force download audio file in ASP.NET using public path

does any one knows how to force the downloa of a file with the "...Save As" window using a public path, for example:
www.domain.com/audio/myfile.mp3
I have found a lot of examples but using a virtual path and noth a public path.
Thank you
You might be able to do some IIS url rewrite voodoo to pull this off. You could easily do some URL routing to make it look like you are downloading from /audio/file.mp3 but are actually pulling the file through a special handler.
But if you are just serving a file with a mimetype there isn't a whole lot you could do -- the client really controls how they handle files when not instructed and you've got no instructions on how to handle it if you are just posting files for download in a directly accessible manner.

download unknown file from Url

In my application I need to download a .fec file from an Url and save it to another location.
I am using the C# Web Client for this.
But when I try to download a .fec file(we cannot view this file in IE or FF) Its downloading
404 page not found file and saving it to myfolder.
I dont know why its downloading a 404 page not found file when the file is existing at specified location.
Is this because .Fec files require a separate program to view it(you need Fec Viewer in order to view .fec files).
If I download a .txt file or .pdf file my program is working fine. I am able to download the
file and save it to my folder.
Any help is appreciated.
Here is my code
public void downloadFile(string url, string saveas)
{
try
{
System.Net.WebClient webclient = new System.Net.WebClient();
webclient.DownloadFile(url, saveas);
}
catch (Exception e)
{
this.ErrorLog(e.Message.ToString());
}
}
You need to set up that extension/mime-type in IIS or add a wilcard for serving unknown file types. By default, IIS won't serve 'unknown files'.
IIS6 - http://support.microsoft.com/kb/326965
IIS7 - http://blogs.iis.net/bills/archive/2008/03/25/how-to-add-mime-types-with-iis7-web-config.aspx
Does a regular browser at least let you download the file if it can't display it directly? If not, then my guess is that your web server is not configured correctly. If the server doesn't have a MIME type associated with that file extension it can prevent the file from being downloaded.

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

Downloading files

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 + "");
}

Resources