Solved

C#  , javascript and FileUpload Asp.net

Posted on 2014-03-04
1
1,612 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

717 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