Download Excel file from Server C# - 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);

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

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

SharpSSH exception when trying to upload file to SFTP remote server

So this is my code, i basically copied and pasted from SharpSSH website.
Sftp oSftp = new Tamir.SharpSsh.Sftp(_ftpURL, _UserName, _Password);
oSftp.Connect(_Port);
oSftp.Put(LocalDirectory + "/" + FileName, _ftpDirectory + "/" + FileName);
oSftp.Close();
When i run this, i get a "first chance exception" on oSftp.Connect(_Port)
A first chance exception of type 'Tamir.SharpSsh.jsch.JSchException' occurred in Tamir.SharpSSH.dll
Does anyone experienced with SharpSSH have any idea why this is happening? I have also tried uploading files to the server using Rubex but it gave me the same error.
First you need to pass only folder path of server and do not specify file name in that path.
oSftp.Put(LocalDirectory + "/" + FileName, _ftpDirectory);
and also make sure you have added below packages from nuget
Install-Package DiffieHellman
Install-Package Org.Mentalis.Security
just check your path of file and path where you want to upload on sftp.
example:
Sftp sftp=new Sftp("host","username","pass");
sftp.Connect();
sftp.Put("path of your local file","path to upload file on sftp server");
sftp.Close();

Using SSH.net in C# to get file content from a remote file

I am using SSH.NET (Renci.SshNet) library to retrieve the contents of a SQL file, but it doesnt seem to handle large files as my computer crashed when i tried to use the following code on a 500MB file.
using (SshCommand command = client.CreateCommand("cat " + filename))
{
filecontent = command.Execute();
SaveFileDialog theDialog = new SaveFileDialog();
theDialog.Title = "Backup - " + filename;
theDialog.Filter = "SQL files|*.sql";
theDialog.FileName = filename;
if (theDialog.ShowDialog() == true)
{
File.WriteAllText(theDialog.FileName, filecontent);
}
}
I then tried to use SFTP to get the file but for some reason i cannot reach the httpd.private folder so i am currently stuck when trying to copy large files.
How can i transfer large files using similar technique to my C# Application for saving? I believe that SFTP is a better solution but i cannot reach the folder the sql is generated.
The SQL file is generated through mysqldump command.
Thanks!
I had to implement the use of the SftpClient that is buildt into SSH.net and iterate over the folders using SftpClient.ListDirectory() and compare the filenames with the file i was looking for. I then opened a stream and downloaded the file using SftpClient.DownloadFile().

Upload Files in C#.net Windows Application

I Want Upload some Files into a Website Host in C#.net Windows Application.what is a Best way To Upload file in Windows Application?
That depends on your server. If it's yours you can make it listen to post requests and upload files through there. If it's not yours there's probably ftp installed, so you can try that.
See here for ftp uploading: http://www.vcskicks.com/csharp_ftp_upload.php
and here for using http post requests: http://en.csharp-online.net/HTTP_Post
i found SVN is the best way so far,
install the client on your local machine and on server. and install the server SNV on your sever.
Server: http://www.visualsvn.com/server/
Client: http://tortoisesvn.net/downloads.html
file upload in C#
HttpPostedFileBase file = this.Request.Files["Upload"];
if (file != null)
{
var fileName = file.FileName;
var fileExtension = Path.GetExtension(fileName);
filePath = fileName.Replace(fileName, emailTemplateImage.token + fileExtension);
var imagepath = Server.MapPath("/Upload/EmailTemplateImage/") + filePath;
if (!System.IO.Directory.Exists(filePath))
{
System.IO.Directory.CreateDirectory(filePath);
}
file.SaveAs(imagepath);
}

Categories