ASP.NET HTTP Get a .tif file and save down - asp.net

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

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.

Insert Image from Symfony path with PHPExcel

I'm trying to insert images in my Excel using the PHPExcel_Worksheet_Drawing of PHPExcel (in this Symfony Bundle) but I get this message all the time:
File http://benefest.local/uploads/persons/image/58af244d3f58c_16143190_10210682923503464_8658615512523719175_n.jpg not found!
Here is my code:
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setPath($path);
$objDrawing->setWidth(30);
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($phpExcelObject->getActiveSheet());
My images are uploaded by VichUploaderBundle, and I'm generating the absolute path through a function. which returns me http://benefest.local/uploads/persons/image/58af244d3f58c_16143190_10210682923503464_8658615512523719175_n.jpg
In a browser, this image is displayed!
Any clue?
Answer is based on my comments to this question.
There is a difference between URI and file path:
URI - used in browser, or being more general in HTTP protocol to get access to some resource via web-server
File path - a link to some file within filesystem on your server/computer/etc.
Your function returns a URI string (http://benefest.local/uploads/persons/image/58af244d3f58c_16143190_10210682923503464_8658615512523719175_n.jpg), which can be used to access the image via a web server.
But PHPExcel_Worksheet_Drawing as well as other libraries use PHP's filesystem functions to get access to some file locally. So you need to get a local file path, instead of URI (in you case it is ./uploads/persons/image/58af244d3f58c_16143190_10210682923503464_8658615512523719175_n.jpg).

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.

Aspx file generation

I am working on an .aspx page.its to download a pdf that is generated in the aspx page.
.but when hosted in Amazon cloud i am getting the message.
"The process cannot access the file because it is being used by another process".
but on the subsequent invocation of the .aspx page i am getting the pdf .
The pdf file is being generated.
Asp.Net uses separeted threads for each request. Probably you use some shared resources to generate pdf and don't clean up them. Therefore parellel requests may fail.
Using block (or calling Dispose() directly) may help.
using (StreamReader reader = new StreamReader(#"C:\My Files\test.txt"))
{
..
}
Also make sure you don't open files with exclusive permissions like this:
FileStream fileStream = new FileStream("test.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None);

ASP.NET: Open File From URL

In ASP.NET, I have an XML file (within my project) that I would like to deserialize. FileStream objects do not allow you to open a file via URL.
What is the easiest way to open the file so that I can deserialize it?
If the file is within your project you just need to get the physical location of the file. You can use Server.MapPath("/yourfile.txt") to get the phyical location and then open it with a filestream.
you can use the XmlDocument.Load(url) method which will load an XML Document from a URL

Resources