Download file and open it without saving it in downloads folder - asp.net

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.

Related

asp.net mvc published how can ı save file in iss server and how can fınd thisfolder path

Im writing a asp.net mvc project it basically create excess and save server im try to do that and its work on my local like this gif user set 2 dates and ım fılter my db sources using this two date and after that ım create excel files and save in folder "Reports" it's perfectly work in my local but its doesn't work published folder doesn't have report folder and ıf ım try create excel in iss its created but ı dont know its saved or if saved save where?
Im save my my excel files like this
string path = Path.Combine(Server.MapPath("~/"), ("Reports\\" + name));
FileInfo excelFile = new FileInfo(path);//new FileInfo(#"C:\Users\amir\Desktop\test.xlsx");
excel.SaveAs(excelFile);
and its donsnt work in Release dont save this files in this folder also this folder(Reports) does not exist and also ım create manually but ıts donst work
after that ım take error from my download function like this
Could not find file 'G:\PleskVhosts\ngsdev.net\httpdocs\Reports\Report_1_1_2020-2-19-04_PM.xls'.
so i try to change my path like this
string path = "G:\\PleskVhosts\\ngsdev.net\\httpdocs\\Reports\\" + name;
and its doesn't work so how can ı do this like my locally any idea or solution is welcomed thanks for help.

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.

asp.net File.Copy Lock

I have an Asp.net page which allows file uploads and then copies the newly uploaded file to another folder. So far so good. Problem is that the copied file is used in the other folder for some other needs (e.g. using FileStream to open the copied file). But asp.net puts a lock on the copied file. I can't even open the copied file using Windows Explorer so long as the Visual Studio's builtin web server is running. I get an error like:
The process cannot access the file 'C:\inetpub\wwwroot.....pdf' because it is being used by another process
Here is partial code:
System.IO.File.Copy(targetFolder_live + "\\" + finalfilename, targetFolder_encr + "\\" + finalfilename);
createpreviewdoc(finalfilename);
////inside the createpreviewdoc function
FileStream stream = new FileStream(targetFolder_encr + "/" + file, FileMode.OpenOrCreate);//Fails with the error!
So what can be done to remove the lock in the same page load? Thanks!
I would attempt the copy in a different fashion.
FileInfo fileInfo = new FileInfo("PathToOriginal");
fileInfo.CopyTo("MyNewLocation");
File.OpenText("MyNewLocation"); // works for me

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

Deleting Application Temp File from ASP.Net

I have a WebPage where I am giving the option to to Export the Form data to PDF. I am creating the PDF at run time and store the PDF in a "PDF" folder which is under my application directory. After creating the PDF with the SessionID name I Call following function to show the PDF file in the new browser window:
ResponseHelper.Redirect(Response, "~/PDF/" + Session.SessionID + ".pdf", "_Blank", "");
This PDF contains the private information related to the logged in user. Therefore, I want a way to delete this PDF file once it is shown in the browser to the user. This is because the IIS server allows whole development team to view this folder which is a security risk, and we can't disallow user to view this folder on the server.
Therefore, if I could delete this file as soon as it is loaded in the browser could be a solution of this security risk.
Can anyone suggest some better ways of deleting this file as soon as possbile from the application?
Thanks,
Praveen
what i guess is you are creating PDF file on runtime using Itext and then you save that PDF file in temp directory to show it to user... why don't you use
Response.WriteFile(PDFFILE);
this will write the whole file on the stream without saving it in temp folder.
One way is to write an ashx handler which streams the pdf to the browser, then deletes it when done.
Another, and much better way, is to simply build the PDF in memory (NOT using session) and stream it as soon as it's ready.
UPDATE
I'm doing this with a slightly modified version of iTextSharp. Basically, iTextSharp performed all of it's operations in memory, then saved the file to disk. I changed this to return the memory stream. All the code is already there, it was really just a line or two that had to change.
Then, I used a response.binarywrite to push the stream directly to the browser. viola! no files on disk.
An ashx handler is just like an aspx page, only it has one entry point and doesn't do all of the page processing garbage. It's light weight and communicates back to the browser by response.write calls.

Resources