Download File via Tamir.SharpSSH.Sftp.Get() - c#

I'm trying to download a file using the Tamir SSH library. I'm able to connect to the remote FTP site, upload files to the site, but I'm getting exceptions when trying to download. I've given IIS_IUSRS full control of the local directory as well as ASPNET. I've tested an I'm able to create a text file in the same local directory I'm trying to download to. Any ideas?
string SFTP_HOST = ConfigurationManager.AppSettings["AccentivFtpHost"];
string SFTP_USERNAME = ConfigurationManager.AppSettings["AccentivFtpUsername"];
string SFTP_PASSWORD = ConfigurationManager.AppSettings["AccentivFtpPassword"];
Sftp client = new Sftp(SFTP_HOST, SFTP_USERNAME, SFTP_PASSWORD);
client.Connect(22);
client.Get("test.txt", "c:\\test.txt");

You probably lack a '/' character in the file directory. You may need to put it either in the Get function call before the "test.txt" like "/test.txt" or at the end of your AccentivFtpHost value in the app config file.

Related

Asp.Net C# Getting Full Path of a File

I am building a web application using WCF. I need to full path of a file to open and upload it to web service.
What I am doing is first opening a file by its full path, then take stream of it and finally upload it to service. My code is below
string Path = Server.MapPath( FileUpload1.PostedFile.FileName);
System.IO.Stream fileStream = File.OpenRead(#Path);
I can't get full path of the file for security reasons.
How can I read the file that users select?
Server.MapPath(FileUpload1.FileName) is the path to a file on the server. You cannot get the full path of the client machine of the file Using the FileUpload.
There is no need for the full client path to access it. You can use the FileBytes
property in order to read the contents of uploaded file.
As others have already suggested there's no reason you need the file path to the client in order to save a file on your server. In the case you need some clearer explanation for this, then please refer to these answers:
https://stackoverflow.com/a/3077008/2196675
https://stackoverflow.com/a/1130718/2196675

Can't access server file

I am trying to access server files, one for reading and other for writing. The following is the path I am setting to achieve this:
var templatePath = Server.MapPath(#"~/Templates/SRG_Template.pptx");
var outputPath = Server.MapPath(#"~/Output/SRG_Document.pptx");
However, I am not able to access them.
Update: In Template folder lies the PowerPoint template which I am reading to create PowerPoint file in Output folder. When I run the application locally, it works fine but on running on server, the ppt file is not created.
I think that your problem is IIS Security Permission.
Did you try that?
You must grant access to IIS_IUSRS user for read, write and modify files on Template and Output folders.
You can read more about this Here.

trying to upload a file to Ftp but getting the error: "file name not allowed"!

So I was trying to upload a 1kb text file to my ftp server but this error comes up:
The remote server returned an error: (553) File name not allowed.
so what's wrong with my code?
WebClient upload = new WebClient();
upload.Credentials = new NetworkCredential("******", "*********");
upload.UploadFile("ftp://xxx.com/public_html", "G:/adress.txt");
It's hard to tell, because it's a server error not a code error. However, as currently written, you're trying to upload the file called adress.txt to become a file named public_html. I suspect there's already a directory with that name, and the conflict is preventing the upload. Try
upload.UploadFile("ftp://xxx.com/public_html/adress.txt", "G:/adress.txt");
instead.
This might not apply to you, but if it is a Linux FTP server:
This may help for Linux FTP server.
So, Linux FTP servers unlike IIS don't have common FTP root directory.
Instead, when you log on to FTP server under some user's credentials,
this user's root directory is used. So FTP directory hierarchy starts
from /root/ for root user and from /home/username for others.
So, if you need to query a file not relative to user account home
directory, but relative to file system root, add an extra / after
server name. Resulting URL will look like:
ftp://servername.net//var/lalala
Instead of:
ftp://xxx.com/public_html
You would need a second slash after the server name in addition to the full file name:
ftp://xxx.com//public_html/adress.txt
I ran into this same issue and it fixed it for me.
Source:
Can't connect to FTP: (553) File name not allowed

get full path through file uploader in Telerik

I am using telerik controls in asp.net
For uploading a file i am using, RadUpload
I gone through following links:
http://www.telerik.com/community/forums/aspnet-ajax/upload/get-full-path-from-uplad-control.aspx#1044702
http://www.telerik.com/community/forums/aspnet-ajax/async-upload/how-to-get-full-path-using-radasyncupload-control.aspx
Made Code As Follows:
for (int i = 0; i < RadUpload1.UploadedFiles.Count; i++)
{
string fileName= Server.MapPath( RadUpload1.UploadedFiles[i].GetName());
}
Its giving me path:
E:\WebBasedNewSoft\NewSoft\NewSoft\colnames.xlsx
this is the path where my solution files are stored.
I wanted to get path of file selected for upload.
Eg. if uploaded file is on c drive , it should give me path:
C:\colnames.xlsx
But its not giving me appropriate path.
What mistake am i making?
What should be appropriate line in for loop??
Please help me.
The file path will NEVER contain the path of the client's computers, because the software on the server is never allowed to take a peek inside the clients computer. If the server was alles to see inside the clients computer we would have a security issue.
The path you are getting is the local path at the server to where the file has been uploaded.

Upload files to a remote server

I need to upload files from my asp.net (C#) page residing in the web server to a remote server.
I managed to upload files to remote server from localhost using this code:
string serverPath = "\\\\xx.xxx.xx.xx\\Folder\\" + FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(serverPath);
But after I published this code to my web server, it stopped working with the error "The network path was not found."
I have looked at a few solutions which suggest using UNC network share and implementing impersonation.
I couldn't figure out how to apply these solutions.
Can someone please give an example, or suggest a simpler solution.
Thanks!!
In FileUpload1.PostedFile.SaveAs(path), path is physical path of file, No Url. You must check:
is Physical folder Exsist?
is You have access to folder?
if answer of both question is true check this code:
string serverPath = #"\\xxx.xxx.xxx.xxx\Folder\";
if (!System.IO.Directory.Exists(serverPath))
System.IO.Directory.CreateDirectory(serverPath);
FileUpload1.PostedFile.SaveAs(serverPath + FileUpload1.FileName);
The account your application runs under must have write permissions to the folder you are trying to upload the file to: \\xx.xxx.xx.xx\Folder\. So you will have to configure the application pool in IIS to run under an account that will have sufficient permissions. Go to the application pool properties in the IIS management console where you will be able to specify an account to be used to run the application. By default it uses a built-in account which won't have any access to shared resources. Take a look at the following article which explains how to do so.
You need a virtual directory on your webserver to upload to. In code you'll have to use Server.Mappath("virtual path") function to get its server path and then save to it.

Categories