How to allow user to download file from asp.net? - c#

I am having problem in asp.net. Actually I am letting my website's visitor to install an window application from that web site. I have added my application without creating installer so it is just an .exe file which is showing a normal form. But the problem is when user tries to download that file an error message is being shown that says that "This file can harm your computer. Do you want to keep this file anyway?" So I don't want to display that message. can you please help me out. Thanks in advance.

This is neither a "problem" of ASP.NET nor the WinForms application. This is a thing that modern browsers do when you download an application or document that might contain (potentially malicious) executable parts. If there was a "this thing won't harm anybody"-flag for downloads, do you think that this would only be set by "nice" developers?

You can try ActiveX to download file from internet explorer browser. This might help. The way to make download in given below.
Example:
public class Downloader
{
public void DownloadFile()
{
using(WebClient webClient = new WebClient())
{
webClient.DownloadFile("http://www.stackoverflow.com/stacks.txt", #"c:\stacks.txt");
}
}
}

Related

Is there a way in Blazor to prompt the user to browse to a local folder and select it for downloading a file?

The user needs to be able to upload a bunch of images which my app will merge into a single PDF. Then the user needs to be able to save that PDF to their local machine. My Blazor C# application uses <InputFile...> to prompt the user to select one or more images from their local machine. The app then creates the PDF file and adds the images to it. I need a way for the user to specify where they want to save the PDF. Or, failing that, I need a way to save it to a pre-determined folder (the application folder or whatever) and a fixed name (such as mergedImages.pdf) and then open the saved file in a browser window from which the user can download it to wherever they want.
I've tried adding a button with an href that points to the downloaded file, but the browser always blocks the link, returning the error: "Not allowed to load local resource: file:///D:/IVG_Blazor/MergeImagesIntoPDF/mergedImages.pdf".
Another way I thought of was to have it write the PDF to the downloads folder and display an icon at the bottom of the page that gives the user the options "Open", "Always open files of this type", "Show in folder", "Cancel". But I don't know how to implement that.
Does anyone know how to give the user easy access to such a file? Thanks.
As I understand, you are able to get the result of the merge operation, I assume it is a Stream or a byte array. And you want the user to be able to download it, and the user should be able to save it in a specific folder.
It seems that the second point is not possible, according to this post : Download A File At Different Location Using HTML5
For the first point, the user be able to download the merged PDF File, all to do is to have the Stream (or byte array), and to have a javascript function to do so.
C# :
private async Task DownloadFileFromStream(Stream fileStream, string fileName)
{
using var streamRef = new DotNetStreamReference(stream: fileStream);
await jsRuntime.InvokeVoidAsync("downloadFileFromStream", fileName, streamRef);
}
Javascript :
async function downloadFileFromStream(fileName, contentStreamReference) {
const arrayBuffer = await contentStreamReference.arrayBuffer();
const blob = new Blob([arrayBuffer]);
const url = URL.createObjectURL(blob);
triggerFileDownload(fileName, url);
URL.revokeObjectURL(url);
}
function triggerFileDownload(fileName, url) {
const anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.download = fileName ?? '';
anchorElement.click();
anchorElement.remove();
}
It will trigger the download procedure of the browser.
Good luck :)
I came across your question when I was trying to solve my problem. I wanted to bring up a window to save a file in which the user could select a folder. Before that, the system automatically saved to the Downloads folder.
As I understood, there was no such possibility before, but now it is possible thanks to filesystem api: https://web.dev/file-system-access/#ask-the-user-to-pick-a-file-to-read.
I found this solution in the answer to the question referenced by Dylan: https://stackoverflow.com/a/70001920/16740180.
Unfortunately, this doesn't work for mobile devices right now, but it works fine for windows. I hope this helps someone.

C# Cant to download JDK15

I'm trying to download JDK15 but i downloading 5307 bytes but this are not the JDK15
using (WebClient wc = new WebClient())
{
wc.DownloadFileAsync(new System.Uri(
"https://download.oracle.com/otn-pub/java/jdk/15.0.1%2B9/51f4f36ad4ef43e39d0dfdbaf6549e32/jdk-15.0.1_windows-x64_bin.exe"),
Path.Combine(directoryPackagesPath, "jdk.exe")
);
}
You downloaded a web page instructing you how to access the download:
You cannot directly download that file. If you open it in another browser, or a incognito/private window, you will see this message:
Sorry!
In order to download products from Oracle Technology Network you must
agree to the OTN license terms.
If you open the downloaded file, most likely it's an HTML with this message.
It is illegal, but if you want learn how to do it for some CTF etc.:
Get query what is send after accepting licence by dev tools in your browser.
Send the exact same query by HttpClient and get cookies from response.
Use this cookies to get the file.
If you want this particular version, you can attach it (if licence allows you to do that) to your program by using installer, by resources or even as normal file in output directory.

Using ASP.NET MVC, is it possible to execute a WPF .exe file after downloading it?

I have an ASP.NET MVC web application with a button to download an .exe file for an existing WPF application. It downloads fine, but when clicked in the browser window, it doesn't execute. How would I fix this?
[HttpGet]
public FileResult downloadFile()
{
var fileName = string.Format("MyApp.exe", DateTime.Today.Date.ToString("dd-MM-yyyy") + "_1");
var tempOutPutPath = Server.MapPath(Url.Content("~/File/")) + fileName;
byte[] finalResult = System.IO.File.ReadAllBytes(tempOutPutPath);
if (System.IO.File.Exists(tempOutPutPath))
System.IO.File.Delete(tempOutPutPath);
if (finalResult == null || !finalResult.Any())
throw new Exception(String.Format("No Files found"));
return File(finalResult, System.Net.Mime.MediaTypeNames.Application.Octet, Path.GetFileName(fileName));
}
This is a security issue, so i would say no. Imagine the disaster if links on malicous sites could download and run programs on the end users pc.
Don't know why you need it, but if you need to launch your wpf app from a browser. Then you could make a link to reference a uri scheme that points to your already installed application. You would have to add it to registry during an install routine or a one time job in your app.
Registering an Application to a URI Scheme in windows 10
Hope this helps
I think this is the closest you can get to run your application from a browser. But making it launch automatically after download is not possible.
like matcsr pointed out what you cannot force a user to execute a file in their download folder. But if what you want is to distribute your WPF app from the web then you need to setup a ClickOnce Web deployment. More info Here: Choose a ClickOnce deployment strategy

How to download magnet link with C#?

Im working on a project which i need to download a magnet link from torrent websites. For example i give the magnet link to a text box and it downloads the content of that magnet link in the specified folder.
I wanted to use monotorrent but it was very complicated and it actually didnt work and it just throw some errors and one of them was "URI prefix is not recognized" and didnt know how to solve it.
and again, all i want to do is to download the magnet link and maybe show the progress with the progress bar
How can I implement such a program with C#?
Using Process.Start() will open the magnet URL with the default application set for handling them. It won't open in your application, but then you'd be writing a torrent application (which is not the easiest task...):
string magnetUrl = "magnet:?xt=urn:sha1: ..."
Process.Start(magnetUrl);
The best way would be programmaticaly downloading torrent file and then run a program with parameter which is the path to torrent file. For example you could use cygwin with rtorrent (command-line torrent client)

Where Can I Find FileUpload Class for Windows Application?

I have this snippet:
protected void ProcessUpload(FileUpload upload)
{
if (upload.HasFile)
{
string fileName = Path.Combine(Server.MapPath("~/Uploads"), upload.FileName);
if (File.Exists(fileName))
File.Delete(fileName);
upload.SaveAs(fileName);
}
}
It is for ASP.NET, i want to run it in Windows Application. I get an error says
FileUpload reference doesn't exist or so. FileUpload resides in System.Web.UI.WebControls
which doesn't belong to Winforms family.
Please note that the file will be saved from desktop to remote file server (not ~/Uploads).
What should i do? What are the alternatives.
You would use My.Computer.Network.UploadFile() in a VB.NET app. The How-To article is here.
What you are asking makes little sense. The FileUpload class handles a file that has been uploaded from a web page to a server, for processing on the server. If you are on a desktop app, the whole concept is removed: why would you upload a file from the desktop to the desktop?
If you want to copy a file, use File.Copy.
If you could give us some more context for how the user is supplying a file, we can be more specific with our answers.
Hope that helps: please post more information so we can get the details worked out.

Categories

Resources