Solved

C#  , javascript and FileUpload Asp.net

Posted on 2014-03-04
1
1,566 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

733 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