Zip file download corrupted - c#

I have a problem with the ".zip" file download. I want to make a game launcher and I need to add the game download and install feature. When I download the packed game with ZipFile.DownloadFile from the server it gets corrupted.
This is the code:
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
// Installation folder will be set in the folder browse dialog
string installationFolder;
// I don't want to show the download url but its in Mediafire
// and I tried to download it from Google Drive
string remoteUri = "the .zip file url";
string fileName = "gamename.zip", myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(remoteUri, Application.StartupPath + "/gamesdownload/" + fileName);
ZipFile.ExtractToDirectory(Application.StartupPath + "/gamesdownload/gamename.zip", installationFolder);
What is wrong with the code?

Found the answer!
I changed from Mediafire to GitHub!
And this is the solution for the error that comes:
The request was aborted: Could not create SSL/TLS secure channel

Related

Download file to browser from SFTP server in ASP.NET using SSH.NET

With SSH.NET, I am trying to trigger a file to be downloaded by the browser instead of directly writing it to the local file storage.
using(var saveFile = File.OpenWrite(#"Downloads")) {
result = client.BeginDownloadFile(remotePath + fileName, saveFile) as SftpUploadAsyncResult;
client.EndUploadFile(result);
}
The above code fails as my the application pool identity doesn't have permission to write files and I cannot give permission as I don't have Admin privileges to my computer.
So better option would be to trigger the download through browser which was working earlier with FtpWebRequest.
But my current task is to convert the FTP download to SFTP download.
Can someone suggest the best way to trigger browser download?
Download the file to HttpResponse.OutputStream:
HttpResponse response = HttpContext.Current.Response;
response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
client.DownloadFile(remotePath + fileName, response.OutputStream);

Download Excel file from Server C#

I am generating an Excel file in a Hangfire background job in my ASP.NET Core MVC site.
I am running into some issues with getting an excel file to download correctly from my website. When I try to download the file when it is running on localhost, it works completely fine. After the code is published and I try to use the feature on the live version, I can't get the file to download.
string previewName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\" + content + " - " + Guid.NewGuid() + ".xlsx";
File.Copy(sourceFileName, previewName);
I originally had written this, thinking I would be able to download the file to the client's My Documents folder. I have learned that this is not the case since Environment.GetFolderpath gets the folder path for the servers computer and not the clients. Since the file is hosted on the server, how can I download that file to the client's computer?
Try using a WebClient to download the file from the site.
using System.Net;
string URL = "https://server.com/files/file.txt";
var FilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\" + content + " - " + Guid.NewGuid() + ".xlsx");
WebClient Client = new WebClient();
Client.DownloadFile(URL, FilePath);
if it is a controller your talking about than instead of copying you can download directly by returning the file in the Controller
return File(yourFilePath);
or you can read the file as bytes and then return it
bytes[] bytesFile = File.ReadAllBytes(path);
return File(bytesFile);

Where to download file on macs using WebClient?

I have an excel file I am downloading using C# and Webclient. The download works but I am wondering if it will also work on a Mac since Macs don't have a C drive.
Here is what I am using:
WebClient client = new WebClient();
client.DownloadFile(file, #"C:\" + guidToken.ToString() + ".xlsx");
The file downloads fine on windows, but if someone is using a Mac, will it work and if not, how do I get it to work?
Use Environmental variables may work.
E.g:
string documentpath = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);

C# WebClient Download file from FTP

I am using the following code to download file from FTP.
NetworkCredential credential = new NetworkCredential(Properties.Settings.Default.FTPUserName, Properties.Settings.Default.FTPPassword);
string inputfilepath = Path.Combine(Properties.Settings.Default.LocalDownloadFolder, file);
string ftpfullpath = Properties.Settings.Default.FTPSite + Properties.Settings.Default.FTPFolder + file;
WebClient request1 = new WebClient();
request1.Credentials = credential;
request1.DownloadFile(ftpfullpath, inputfilepath);
Values of the first two vaiables is:
E:\FTPDownloads\CardholderManagementReport_1030_2012-12-11.xls
ftp://abc.com/AKSHAY/CardholderManagementReport_1030_2012-12-11.xls
It shows error as :
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
EDIT:
I can see that the file is indeed present there, credentials are ok and I can download it using FileZilla
The error 550 returned by the FTP server indicates that the user you are using to try and access the file with does not have permission to access the file.
Either use some other set of credentials that has access to the file or change the permissions on the file to allow access.
This makes it work.
ftp://abc.com/%2f/AKSHAY/CardholderManagementReport_1030_2012-12-11.xls
Explaination is also there.

Download Latest File from SharePoint Server

I have very little idea on sharepoint. Need a start,how to access the share point site and download the latest file avilable in a particular folder.
I am using ASP.Net/C#.Net and Visual Studio 2008 to download file.
What is requirement means any particular libraray/update/pathes?
How can i get to know path of webservice ? (I only know the direct URL)
Is it possible to access the metadata of the file.
Any help will be appreciated.
Thanks
I found this on a web search at some point. This is what I ended up using.
var remotefile = #"https://pathtoremotefile";
var localfile = #"C:\pathtolocalfile";
using(WebClient wc = new WebClient())
{
wc.Credentials = CredentialCache.DefaultNetworkCredentials;
// or new System.Net.NetworkCredential("user","pass","domain");
wc.DownloadFile(remotefile,localfile);
}

Categories