Solved

C#  , javascript and FileUpload Asp.net

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

840 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