Solved

C#  , javascript and FileUpload Asp.net

Posted on 2014-03-04
1
1,502 Views
Last Modified: 2014-03-21
I have a FileUpload Control on a webpage.  

On a button I have a OnClientClick that execute
a JavaScript that let the user enter e-mail adresses to send some mail to.
than it calls an WebMethod that sends the e-mail and then update the
database with some information.

I need to do the actual fileupload (to the database)  inside the webmethod
so I wounder how I can get access to the fileupload
from my webmethod?




 


My webmethod today:

[WebMethod(EnableSession = true)]
public static string SendMail(string id, string emails, string comments)
    {
        User u;
        u = (User)HttpContext.Current.Session["UserInfo"];
        if (emails.EndsWith(";"))
            emails = emails.Substring(0, emails.Length - 1);
       

        int res = MailManager.Send(int.Parse(id), u, comments, emails);
        return res.ToString();
    }
0
Comment
Question by:pucko73
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39905316
Using a generic handler :
public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        User u;
        u = (User)HttpContext.Current.Session["UserInfo"];

        string id = context.Request.QueryString.Get("id");
        string emails = context.Request.QueryString.Get("emails");
        string comments = context.Request.QueryString.Get("comments");

        if (emails.EndsWith(";"))
        {
            emails = emails.Substring(0, emails.Length - 1);
        }

//http://msdn.microsoft.com/en-us/library/system.web.httppostedfile.saveas(v=vs.110).aspx
        HttpPostedFile attachment = context.Request.Files[0];
        //string filename = "C:\\Temp\\" + attachment.FileName;
        //attachment.SaveAs(filename);

        int res = MailManager.Send(int.Parse(id), u, comments, emails, attachment);

        context.Response.ContentType = "text/plain";
        context.Response.Write(res.ToString());
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    } 

}

Open in new window


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery.form/3.49/jquery.form.js"></script>
    <script type="text/javascript">
        jQuery(function ($) {

            var success = function (data) {
                alert(data);
            }

            $("#<%= form1.ClientID %>").ajaxForm({ url: "Handler.ashx", success: success });

        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:HiddenField Value="123AZE" runat="server" ID="id" />
        <br />
        <asp:TextBox Text="leakim971@ee.com,pucko73@ee.com" ID="emails" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox Text="My comment" runat="server" ID="comments" TextMode="MultiLine"></asp:TextBox>
        <br />
        <asp:FileUpload ID="attachment" runat="server"></asp:FileUpload>
        <br />
        <asp:Button Text="SEND" ID="send" runat="server"></asp:Button>
    </div>
    </form>
</body>
</html>

Open in new window

0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
InputLanguage 1 26
Passport Expiry 3 44
Need Help adding an array and loop to a React file 7 27
RLDC Reporting in Visual studio 11 15
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question