download unknown file from Url - asp.net

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.

Related

Download file and open it without saving it in downloads folder

We try to download a file from the web server (.NET 5 / MVC) and open it automatically on the client side with the specific appliation without saving the file on the local disk. In our case we download a CAD file (*.dwg) and open it with DWG TrueView.
The file gets downloaded and application will be started as well. But every time we click on a file in out web application, the browser save a copy of the file in the downloads folder.
What is the correct approach to solve this problem?
Citrix does the same with ICA files - once I tell the browser it should open ICA files, it will not save the files on the disk.
This is roughly how the code looks like:
public FileStreamResult DownloadFile()
{
var fs = new FileStream("myFilePath....", FileMode.Open, FileAccess.Read);
return new File(fs,"application/octet-stream","myFile.dwg");
}
We also tried all sorts of content-types without success.

download link give me filenotfound exception

in my controller, this is the code i have
public FileResult Download(string file)
{
var vFullFileName = HostingEnvironment.MapPath("~/App_Data/Files/");
var files = uploadedfileRepository.AllIncluding();
string filename = (from f in files
select f.FileName).First();
return File(Path.Combine(vFullFileName, filename), "application/csv", filename);
}
I put the breakpoint and the file point to the right directory, but why still give me File not found exception?
and in my view this is what i have
<td>
#Html.ActionLink("Download", "Download", new { id = item.FileName})
</td
Your action method have a parameter with name file. But your are HTML will have a parameter/ query string called id, instead od file, So change your view code to
#Html.ActionLink("Download", "Download", new { file= item.FileName})
Also to get the path, try this
string fullFilePath=Path.Combine(Server.MapPath("~/App_Data"),filename)
return File(fullFilePath,"application/csv",filename);
App_Data is for database files that are accessed by MS SQL Server, and possibly your application's own data files (like a Lucene index).
IIS (and ASP.NET) are configured to block any client requests to that directory.
The solution is just to move the files to another directory. Just create a new folder in your site's root (say "CsvFiles") and link to that.
That said, why don't you serve up a HTTP 301 Redirection (or even a direct link) to the CSV files instead of serving them via your application?
UPDATE: This answer is incorrect because by serving the file contents via an MVC File response the user's browser doesn't acutally access the App_Data directory.

Access to files on a server (asp.net, webmatrix)

I want to read a *.txt file and edit this file. I use Webmatrix and it works on my computer. But after publishing it to a server (Web deploy) it doesn't work anymore.
string transmission;
string path;
path = "E:\\Documents\\My Web Sites\\Trackercontrol v2\\backup.txt";
public int insert()
{
using (StreamReader sr = File.OpenText(path))
{
while ((transmission = sr.ReadLine()) != null)
{
// etc.
}
}
}
I published this, adjusted the path to the .txt file, but the try/cath method told me that the access to the path is denied. I think reading out the file isnt the problem but editing or clearing the file makes this problem.
How can I fix this? Thank you very much!
You gave the path of your local drive, which may be or sure does not exist on the server. First upload the file on the server and then read it. You can get the correct path as
string path;
path = Server.MapPath("~/filename.txt"); // Considered file is placed at Root of your site
Check what account is the application pool running under?
Make sure that this account has read/write access to the file in concern.
Also, it would be a good idea to use
var path = Server.MapPath(relativePathToFile);

Getting file InputStream from 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.

ASP.NET HTTP Get a .tif file and save down

Hey I am currently using a component in which I need to load a path to a file.
Now my plan was originally to just use the URL to the file since they are hosted on the web, but sadly this component can only load file saved down locally.
So now I am thinking before I can load the file in this component I need to save down the file first,
So how would I go about this in .net
1) Call URL
2) Save down the file to a local folder
Download it with the WebClient class:
using (WebClient client = new WebClient())
{
client.DownloadFile("http://the.address.com/TheImage.tiff", #"C:\TheImage.tiff");
}

Resources