Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • TENNO 13 posts 73 karma points
    Nov 05, 2013 @ 18:54
    TENNO
    0

    Allow end user to Upload Files

    Hi,

    I want to create a form that "end user" able to upload file This file will be sent in email as attachment

    I want to know how is it possible,

    and if it's not, is it possible to allow "end user" to upload to pre-selected media folder.

    This is my try, but there is 2 problem with it:

    1. the file path is not valid because the code runs on server not "end user" machine.
    2. the < input type="file" /> don't return the full file path

    -

    if (IsPost)
    {
        //using: string cv = Request["cv"]; don't work
        //just return the file name without the path.
    
        var uploadedFile = Request.Files[0];
        var fileName = Path.GetFileName(uploadedFile.FileName);
    
    
        var mailMessage = new MailMessage("emailFrom", "emailTo")
        {
            Subject = emailSubject,
            Body = "some text"
         };
    
        var smtpClient = new SmtpClient("mail.123.com", 587)
        {
            Credentials = new NetworkCredential("username", "password"),
            EnableSsl = false
        };
    
        System.Net.Mail.Attachment attachment;
        attachment = new System.Net.Mail.Attachment(fileName );
        mailMessage.Attachments.Add(attachment);
    
        smtpClient.Send(mailMessage);
    }
    

    HTML Code:

    <form method="post" action="">
        <fieldset>  
            <input type="file" id="cv" name="cv" accept="application/pdf"/>
            <input type="submit" id="submit" value="Send" />
        </fieldset> 
    </form>
    

    Thanks in advance!

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Nov 05, 2013 @ 20:56
    Andy Butland
    100

    Hi Tenno

    You'll need to save the uploaded file to disk first - on the server - using Request.Files[0].SaveAs(pathToFile) (see example here).  And then you can get the file path to that to initialise the System.Mail.Attachment class.

    Just noting also you'll need an enctype="multipart/form-data" on your <form>.

    Andy

  • TENNO 13 posts 73 karma points
    Nov 06, 2013 @ 16:19
    TENNO
    0

    Hi Andy,

    Thanks for you reply,

    I Got and an exception "Index Out of Range" in this line.

    Request.Files[0];
    

    which mean that there is no files uploaded.

    BTW, this line executed in the page post back

    if (IsPost)
    {
        Request.Files[0];
    }
    

    Do you know why?

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Nov 06, 2013 @ 17:12
    Andy Butland
    0

    Suggests no file was uploaded as you say... hence there's nothing at index = 0 (i.e. the first position) in the Request.Files collection.  Did you add the enctype="multipart/form-data" attribute to your form?

  • TENNO 13 posts 73 karma points
    Nov 06, 2013 @ 18:58
    TENNO
    0

    I missed that,

    Thanks @Andy for your time, it works


    I used this Constructor Attachment(Stream, String) to avoid saving file on server

    so final code is: -without validation-

    HttpPostedFileBase file = Request.Files[0];
    string fileName = file.FileName;
    
    // some code for creating MailMessage and SmtpClient objects
    
    Attachment attachment = new Attachment(file.InputStream, fileName);
    mailMessage.Attachments.Add(attachment);
    
    smtpClient.Send(mailMessage);
    
  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Nov 06, 2013 @ 20:26
    Andy Butland
    0

    Good idea - much better.

Please Sign in or register to post replies

Write your reply to:

Draft