Send email / file upload

Please post public support tickets here. Note: for private support tickets, please send an email to support@cshtml5.com instead.
ScottM
Posts: 38
Joined: Wed Mar 27, 2019 7:04 am

Send email / file upload

Postby ScottM » Mon May 20, 2019 8:40 am

(I maybe posted this in the wrong section last week. Revised with more explanation.)

I put in code to upload a file from the user. Now I'd like, if possible, to do two things. First, and most important:

1. Send an email with form responses and the uploaded file attached. I looked for MailMessage that's in the WPF stack, but no dice here, unless I am missing something.

Then

2. Save the uploaded file and form responses to a folder on the server. I've tried WebClient and WebClientWithCredentials. I get "No 'Access-Control-Allow-Origin' header is present on the requested resource"

Code: Select all

WebClient webClient = new WebClient();
webClient.UploadStringAsync(new Uri("http://www.heavenword.com/sv2dos/firsttest.txt"), "testing123");

or

Code: Select all

WebClientWithCredentials webClientWithCredentials = new WebClientWithCredentials();
webClientWithCredentials.CredentialsMode = CredentialsMode.Enabled;
webClientWithCredentials.Encoding = Encoding.UTF8;
webClientWithCredentials.Headers[HttpRequestHeader.AcceptEncoding] = "application/xml";
webClientWithCredentials.UploadStringAsync(new Uri("http://www.heavenword.com/sv2dos/firsttest.xml"), "testing123");


I believe I enabled write access to the above sv2dos folder on the server.

Perm.png
Perm.png (12.72 KiB) Viewed 12514 times


I realize I'm asking for more server-side integration than the thick client, but I thought I would find examples here in the forum on how to do that. I ran several searches, but came up empty.

Any suggestions?

JS-Support @Userware
Site Admin
Posts: 1142
Joined: Tue Apr 08, 2014 3:42 pm

Re: Send email / file upload

Postby JS-Support @Userware » Tue May 21, 2019 1:41 am

Hi,

While incomplete, here are some elements that you may find useful:

- Sample code to compose an email from a client-side app (which opens the default mail client) can be found at:
http://forums.cshtml5.com/viewtopic.php?f=7&t=1463
For the attachment you would need to add (to the email body) a link to the file uploaded to the server.

- The message "No 'Access-Control-Allow-Origin' header is present on the requested resource" means that there is an issue with cross-domain calls. More information on this topic can be found at:
http://www.cshtml5.com/links/wcf-limitations-and-tutorials.aspx#cors

If you need to pass a security token to each request, you should indeed use the "WebClientWithCredentials" and set the "CredentialsMode" to "Enabled".

As for the server-side code, the recommended approach for receiving the file is to implement an "ashx" handler that receives the file (server-side). Alternatively, you can pass the byte array in a SOAP call too.

Hope it helps.
Thanks.

ScottM
Posts: 38
Joined: Wed Mar 27, 2019 7:04 am

Re: Send email / file upload

Postby ScottM » Tue May 21, 2019 10:54 am

Thank you for your help.

ScottM
Posts: 38
Joined: Wed Mar 27, 2019 7:04 am

Re: Send email / file upload

Postby ScottM » Tue May 21, 2019 2:08 pm

OK, so if I launch the CSHTML5 app from xyz.com, and I try to UploadString to the same xyz.com, the CORS shouldn't be a problem, right? I'm OK with that, if I can just upload a string (JSON) to a folder on xyz.com.

But I can't get it to work. Obviously in the simulator, I get the "No 'Access-Control-Allow-Origin'" error, but if I launch from xyz.com, shouldn't this code work?

Code: Select all

WebClient webClient = new WebClient();
string sReply = webClient.UploadString(new Uri("https://xyz.com/sv2dos/firsttest.txt"), "testing123");


Nothing ends up in that folder.

I also tried (among lots of other things) to use FTP:

Code: Select all

string sReply = webClient.UploadString(new Uri("ftp://ftp.xyz.com/incoming/firsttest.txt"), "STOR", "testing123");


where /incoming was set up for anonymous ftp. It feels like I should be using

Code: Select all

webClientWithCredentials.Credentials = new System.Net.NetworkCredential(userName, password);


with another version I tried:

Code: Select all

WebClientWithCredentials webClientWithCredentials = new WebClientWithCredentials();
webClientWithCredentials.CredentialsMode = CredentialsMode.Enabled;
webClientWithCredentials.Encoding = Encoding.UTF8;
webClientWithCredentials.Headers[HttpRequestHeader.AcceptEncoding] = "application/xml";
string sReply = webClientWithCredentials.UploadString(new Uri("ftp://ftp.xyz.com/incoming/firsttest.txt"), "STOR", "testing123");
DebugInfo.Text = sReply;


but I don't have .Credentials or NetworkCredential that I can find.

I also tried making \sv2dos a sub-folder of the folder I launched the app from, to no avail.

This is being hosted on BlueHost, not sure if ashx is supported, or if now, CORS is even my problem.

Sorry to be a pest, it seems like 2 to 10 lines of code here and I should be done, but after spending most of the afternoon, I guess not.

I'm going to go try to get the email working.

ScottM
Posts: 38
Joined: Wed Mar 27, 2019 7:04 am

Re: Send email / file upload

Postby ScottM » Tue May 21, 2019 3:23 pm

Update, didn't do email, built a test WPF app. This works:

Code: Select all

WebClient webClient = new WebClient();
webClient.Credentials = new NetworkCredential("name", "password");
webClient.UploadStringAsync(new Uri("ftp://ftp.xyz.com/uploads/string.txt"), "STOR", "testing123");


I'm pretty sure your webclient UploadString (Async or not) allows the "STOR" to force the ftp, but I don't see how I can add the credentials.

If there is anyway I can just those credentials in there (with the real values), I could move on.

User avatar
rthomas
Posts: 10
Joined: Mon Feb 18, 2019 7:03 am
Location: Lille, France

Re: Send email / file upload

Postby rthomas » Tue May 21, 2019 10:42 pm

Hi Scott

Did you try to trace webrequest to see exactly what is going on?
If you use CSHTML5 v2 you can directly using browser F12/Network view
For CSHTML5 v1 you should use fiddler

Remi

ScottM
Posts: 38
Joined: Wed Mar 27, 2019 7:04 am

Re: Send email / file upload

Postby ScottM » Wed May 22, 2019 7:02 am

Remi:

Thanks for the reply and hints. I'm using v2 (the latest, I think) and here is what I see:

F12Results.png
F12Results.png (74.49 KiB) Viewed 12480 times


the code for that was:

Code: Select all

WebClient webClient = new WebClient();
string sReply = webClient.UploadString(new Uri("https://musserportal.com/salesportal/sv2dos/firsttest.txt"), "testing123");


and that folder is there, and I made sure it had write permission in FileZilla:

sv2dos.png
sv2dos.png (55.98 KiB) Viewed 12480 times


so if you can shed any light on what I'm doing wrong, please do.

Thanks.

ScottM
Posts: 38
Joined: Wed Mar 27, 2019 7:04 am

Re: Send email / file upload

Postby ScottM » Wed May 22, 2019 12:19 pm

Still fighting this. Tried poking some more around an ftp solution using javascript. Got a token from http://ftp.apixml.net/ thinking I could just do a javascript call via interop, which seems like it should work, but I'm not sure how to push a file into the javascript. It seems like it could look something like this:

Code: Select all

CSHTML5.Interop.ExecuteJavaScript(@"
< script src=c'http://ftp.apixml.net/ftp.js'>
</script >
Ftp.upload('token_goes_here', 'magic file association goes here?'
");


So, still flailing, I guess.

User avatar
rthomas
Posts: 10
Joined: Mon Feb 18, 2019 7:03 am
Location: Lille, France

Re: Send email / file upload

Postby rthomas » Fri May 24, 2019 5:24 am

Hi Scott,

You can't upload a file like this.
You need a server that accept POST
At minimum you need WebDav or FTP server but no sure about FTP upload using XMLHttpRequest

It was planning to do a demo on how to write this kind of server using asp.net core 2.2 and how to consume web API from CSHTML5 project and XAML project sharing same client code.

I'll try to do it soon.
A good pointer if you want more info is here
https://stackoverflow.com/questions/148 ... ftp-server

Remi

ScottM
Posts: 38
Joined: Wed Mar 27, 2019 7:04 am

Re: Send email / file upload

Postby ScottM » Fri May 24, 2019 6:46 am

Remi:

Thanks for the reply.

I tried implementing the process.php file (modified for my file types) to get a POST api

https://www.taniarascia.com/how-to-uplo ... t-and-php/

on the server, and calling it (renamed nuplload.php from Postman) I get a 200, but the file does not load.

Postman.png
Postman.png (46.44 KiB) Viewed 12452 times


I'm going to see if I can do something with an Azure function or AWS lambda.

ScottM
Posts: 38
Joined: Wed Mar 27, 2019 7:04 am

Re: Send email / file upload

Postby ScottM » Tue May 28, 2019 11:04 am

I need to do some more testing, but I think I got it working with an Azure function saving the file to Azure BlobStorage. It eliminates any CORS issue.

Thanks.


Return to “Technical Support”

Who is online

Users browsing this forum: No registered users and 30 guests

 

 

cron