How do I receive the file on server side? - c#

I have a code like this in my local exe to upload a file in a certain url:
private static void SaveTToWeb()
{
try {
WebClient client = new WebClient();
client.Credentials = CredentialCache.DefaultCredentials;
client.UploadFile("www.something.com/uploadreceiver.aspx", "POST", "text.txt");
client.Dispose();
myFile = null;
urlForUpload = null;
}
catch (Exception err) { Console.write("error: " + err.Message(); }
}
my question is, in the server side "uploadreceiver.aspx" page, what code should i use to actually receive the file? thank you!

The CSASPNETRemoteUploadAndDownload sample shows how to upload files to and download files from remote server in an ASP.NET application.
You can download the sample code or read more here http://code.msdn.microsoft.com/CSASPNETRemoteUploadAndDown-a80b7cb5

Related

Download file from google drive on server

I use an javascript picker to get file from my google drive, it's work well and i get then download url and acces_token from my drive
I would like to download bytes array from this file from my server, then i path the url and acces_token to it (with ajax), no problem
on server i would get data with this code (it's worked !!!)
public void DownloadFile(string url, string AccessToken)
{
try
{
byte[] BB;
using (WebClient wc = new WebClient())
{
wc.Headers.Add("Authorization", "Bearer " + AccessToken);
BB = wc.DownloadData(url);
}
}
catch (Exception ex)
{
throw ex;
}
}
now i get an 403 error ??
url are like "https://content.googleapis.com/drive/v2/files.....?key=mykey....."
what's changed on google server ?
thanks
If you want to download a file using the Drive API and setting the access token in the header, you have to do it by calling this url:
https://www.googleapis.com/drive/v2/files/[file-Id]?alt=media
As it's told in the Response section in the Files: get endpoint. It's really important the url parameter alt=media in order to make it work. Don't use an API Key.

Device not configured when downloading remote PDF in ASP.NET Core

I'm trying to download a PDF from a remote URL in order to add it to a mail attachment.
My application is a .NET Core 2.1 API. The URL is a single page PDF on AWS cloudfront.
try
{
using (var httpClient = new HttpClient())
{
string url = AttachmentPath + "/" + AttachmentFilename;
byte[] bytes = await httpClient.GetByteArrayAsync(url);
string fileContentsAsBase64 = Convert.ToBase64String(bytes);
msg.AddAttachment(AttachmentFilename, fileContentsAsBase64, "application/pdf");
}
}
catch (Exception ex)
{
}
I'm getting the following error:
{System.Net.Sockets.SocketException (6): Device not configured at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port,
CancellationToken cancellationToken)}
What am I doing wrong?
Update
Thanks to the link shared below, I've modified the code as follows:
private static HttpClient httpClient = new HttpClient();
try
{
string url = AttachmentPath + "/" + AttachmentFilename;
// The following line causes the exception
byte[] bytes = await httpClient.GetByteArrayAsync(url);
string fileContentsAsBase64 = Convert.ToBase64String(bytes);
}
catch (Exception ex)
{
}
The exception occurs at await httpClient.GetByteArrayAsync.
It seems that your S3 Config is missing. You need to include your S3 config in the HttpClient possibly mapped to header.
there is similar issue on GitHub#1014 whereas this uses AmazonS3Client.

Exception during a WebClient request for a FTP download

I'm trying to develop a function that downloads a file in a FTP directory. I try this way
static void descargarFic(string ficFTP, string user, string pass, string dirLocal)
{
Library.WriteErrorLog(ficFTP + " -> " + dirLocal);
try
{
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(user, pass);
client.DownloadFile(ficFTP, dirLocal);
Library.WriteErrorLog("Done");
client.Dispose();
}
catch(Exception e)
{
Library.WriteErrorLog(e.Message);
}
}
The WriteErrorLog write on a file the string passed. So, I'm calling that function in this way:
descargarFic("ftp://something-here/incoming/file.xml.gz", "anonymous", "password", #"C:\folder1\file.xml.gz");
But I receive the exception Exception during a WebClient request for a FTP download when I try to download it.
I'm pretty sure that it's some kind of problem with the URI, but I didn't realize where it is.
EDIT: Using Library.WriteErrorLog(e.InnerException);
The exception given is
The given access path's format is not supported.

writing files to a remote http server

I have created a virtual directory with read and write access to everyone on Machine B. I am running web application 1 on Machine A. Now the requirement is to upload a file to remote http location from this web application. Machine A web application i is configured with anonymous authentication.
i am not able to implement the above requirement successfully. Please suggest whether this approach is correct or not?
public override bool UploadFile()
{
byte[] postData;
try
{
postData = this.FileData;
using (WebClient client = new WebClient())
{
client.Credentials = CredentialCache.DefaultCredentials;
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.UploadData(this.UrlString, "PUT", postData);
}
return true;
}
catch (Exception ex)
{
throw new Exception("Failed to upload", ex.InnerException);
}
}
i have tried the example given in the below location
http://code.msdn.microsoft.com/CSASPNETRemoteUploadAndDown-a80b7cb5
unfortunately i could not able to make it work. I am using IIS7 on both machine A & B
Please suggest.

Uploading data doesn't really upload and with no error in C#

I have this code, but I get the error described in the title:
"The remote server returned an error: (405) Method Not Allowed."
** I've replaced PUT by POST now**
If I replace "PUT" by "POST" it seems to work as I don't get an error, but it does not upload any file.
I'm trying to upload a file to a document library in sharepoint (office 365)
public static void UploadTest()
{
WebClient w = new WebClient();
w.Credentials = new NetworkCredential("username", "password");
var ua = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
w.Headers["Accept"] = "/";
w.Headers.Add(HttpRequestHeader.UserAgent, ua);
byte[] bFile = System.IO.File.ReadAllBytes(#"C:\t.txt");
string ulr = #"http://www.website.com/uploadfolder/";
System.Uri oUri = new System.Uri(ulr);
try
{
w.UploadData(oUri, "POST", bFile);
w.UploadDataCompleted += new UploadDataCompletedEventHandler(oWebClient_UploadDataCompleted);
Console.WriteLine("Uri:" + oUri);
}
catch (Exception ex)
{
throw ex;
}
finally
{
Console.ReadLine();
}
}
There is a problem regarding access with credentials and logging in.
Something seems to go wrong when I try to connect, as I can download a corrupt file when I remove the w.Credentials = new NetworkCredential(user,pass);
I'm trying a total new way around now, thanks for replying.

Categories