Solved

C#  , javascript and FileUpload Asp.net

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now