I've come across a few forums posts where people have asked this question under special circumstances.
The scenario: A web application with some sort of web service or a simple web handler needs to request multiple files and email them, but where the data is posted from disparate clients. These clients could be static html forms, desktop applications, etc.
The following is one simplified example of how you might achieve this..
First, let's look at the html form.
<form id="MyForm" method="post" enctype="multipart/form-data" action="http://mydomain.com/FeedMe.ashx">
<p>Name: <input type="text" id="name" name="name" ></input></p>
<p>Email: <input type="text" id="email" name="email" ></input></p>
<p>File 1: <input type="text" id="file1" name="file1" ></input></p>
<p>File 2: <input type="text" id="file2" name="file2" ></input></p>
<p><input type="submit" id="submit" name="submit" value="Submit" ></input></p>
</form>
We are submitting two files, file1 and file2, to a web handler called FeedMe.ashx.
Now let's take a look at the code within our web hander:
using System;
using System.Collections;
using System.Web;
using System.Net.Mail;
namespace MyWebHandler
{
public class EmailFiles : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string name = context.Request.Form["name"]
string email = context.Request.Form["email"];
MailMessage mail = new MailMessage();
mail.From = new MailAddress(email, name);
mail.Subject = “You haz a filez!”;
mail.Body = “You can haz multiple filez now!”;
mail.To.Add(new MailAddress(“me@email.com”, “me”));
HttpFileCollection MyFiles context.Request.Files;
foreach (String fileKey in MyFiles)
{
mail.Attachments.Add(new Attachment(MyFiles[fileKey].InputStream, MyFiles[fileKey].FileName.ToString()));
}
SmtpClient client = new SmtpClient("localhost");
client.Send(mail);
context.Response.Write(“response to client”);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Looking at the web handler, first we request our name and email form elements from the form submission:
string name = context.Request.Form["name"]
string email = context.Request.Form["email"];
Next, we create our email:
MailMessage mail = new MailMessage();
mail.From = new MailAddress(email, name);
mail.Subject = “You haz filez!”;
mail.Body = “You can haz multiple filez now!”;
mail.To.Add(new MailAddress(“me@email.com”, “me”));
Now, we'll request the submitted files from context and assign them to MyFiles.
Using a foreach loop, we loop through the collection and grab the key for each posted file.
Once we have the key, we Add a new mail attachement, passing the InputStream of MyFiles for the given key as the File, along with the name of that file.
(Note, you will want to filter out the local path in the FileName)
HttpFileCollection MyFiles context.Request.Files;
foreach (String fileKey in MyFiles)
{
mail.Attachments.Add(new Attachment(MyFiles[fileKey].InputStream, MyFiles[fileKey].FileName.ToString()));
}
Once we've cycled through all the files posted and attached them to the email, we'll send the email:
SmtpClient client = new SmtpClient("localhost");
client.Send(mail);
context.Response.Write(“response to client”);
24 comment(s) so far
My partner cuts up polystyrene (plastic foam) as a way of recycling materials for packaging. It's very efficient but the build up of static electricity slows the job down and drives her crazy.
Thanks for the information. I have been trying to figure out how to do this with my payroll company. I appreciate your time and comments.
Using a foreach loop, we loop through the collection and grab the key for each posted file.
Depends whether you want to download files more than once a day or simultaneousness. You can try Rapid8 dot com but its full of viruses so make sure you have antivirus. Or search on youtube for a premium account generator.
I have been trying to figure out how to do this with my payroll company.
These clients could be static html forms, desktop applications, etc.
very efficient but the build up of static electricity slows the job down and drives her crazy.
Now, we'll request the submitted files from context and assign them to MyFiles.
name and email form elements from the form submission:
You can try Rapid8 dot com but its full of viruses so make sure you have antivirus. Or search on youtube for a premium account generator.
we'll request the submitted files from context and assign them to MyFiles.
I appreciate everything you have added to my knowledge base.Admiring the time and effort you put into your blog and detailed information you offer!
It's called server-side programming. Languages like ASP and PHP allow HTML to be generated
dynamically.
I've been trying to explore my phone settings and internet blogs on how to send multiple files, but when I try it it keeps on sending only one file. I have to send it one by one. It's really frustrating, especially when I need to send more than 20 files. Could somebody help me? It would really help. :) Thanks.
The following is one simplified example of how you might achieve this..
submitted files from context and assign them to MyFiles.
Just browse for your favorite Ethiopian artist and enjoy his or her music videos. To your convenience mgport has alphabetically sorted the ingredients of Ethiopian Music Videos by artists name to ease browsing.
The following is one simplified example of how you might achieve this..
I am sure you are planning to download from some site and upload to others and thinking you will be paid for all those. Forget all that man. No one is going to pay you a single penny.
If you'd like to send multiple files, then connect the devices to a computer and do it all on there by dragging the files from one folder to another.
The following is one simplified example of how you might achieve this..
I've already seen answers about saving INDIVIDUAL files, which I've seen already. But I just got a flash-drive and I need to transfer about 150 files from G-Docs to my flash drive. When I select multiple files, there is only a save as HTML option....Thanks..
I have been searching for quite some time for information on this topic and no doubt your website saved my time and I got my desired information. Your post has been very helpful. Thanks.
Using a foreach loop, we loop through the collection and grab the key for each posted file.