Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to trigger an event from fileupload?

How to trigger an event from fileupload?

Scheduled Pinned Locked Moved ASP.NET
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    turbosupramk3
    wrote on last edited by
    #1

    I'm new to ASP, but not c#. The fileupload control seems to be different than the rest of my controls, I cannot seem to link it to my c# code. Do I need to create an event listener for it? Or a background worker process thread to monitor something from it? I'm sure this is caused by me not understanding how the html and c# code work. How can I communicate between the fileupload control and my c sharp code in the background? I tried to put a javascript function into the head portion but it does not appear to be called and when I put a break point in there to test it, it does not hit the break point. Thanks

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="True"
    CodeBehind="Default.aspx.cs" Inherits="exchangePictureUpdater.exchangePictureUpdater" %>

    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

    <script src="jquery-2.2.4.js"></script>
    <script>

          function loadimage() {
              alert($("#thefile").prop('files'));
          }
    
          function readURL(input) {
              if (input.files && input.files\[0\]) {
                  var reader = new FileReader();
    
                  reader.onload = function (e) {
                      $('#imgBox')
                          .attr('src', e.target.result)
                      <%--.width(150)
                          .height(200); --%>
                      $("#<%=tbxPictureHeight.ClientID %>").val("");
                      $("#<%=tbxPictureWidth.ClientID %>").val("");
                      $("#<%=tbxPictureFileSize.ClientID %>").val("");
    
                      $('#imgBox').on('load', function () {
                          var height = this.naturalHeight,
                              width = this.naturalWidth;
                          $("#<%=tbxPictureHeight.ClientID %>").val(height);
                          $("#<%=tbxPictureWidth.ClientID %>").val(width);
                          var imgpath = document.getElementById('theFile');
                          //if (!imgpath.value==""){
                          //var img=imgpath.files\[0\].size;
                          //var imgsize
    
    J F 2 Replies Last reply
    0
    • T turbosupramk3

      I'm new to ASP, but not c#. The fileupload control seems to be different than the rest of my controls, I cannot seem to link it to my c# code. Do I need to create an event listener for it? Or a background worker process thread to monitor something from it? I'm sure this is caused by me not understanding how the html and c# code work. How can I communicate between the fileupload control and my c sharp code in the background? I tried to put a javascript function into the head portion but it does not appear to be called and when I put a break point in there to test it, it does not hit the break point. Thanks

      <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="True"
      CodeBehind="Default.aspx.cs" Inherits="exchangePictureUpdater.exchangePictureUpdater" %>

      <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

      <script src="jquery-2.2.4.js"></script>
      <script>

            function loadimage() {
                alert($("#thefile").prop('files'));
            }
      
            function readURL(input) {
                if (input.files && input.files\[0\]) {
                    var reader = new FileReader();
      
                    reader.onload = function (e) {
                        $('#imgBox')
                            .attr('src', e.target.result)
                        <%--.width(150)
                            .height(200); --%>
                        $("#<%=tbxPictureHeight.ClientID %>").val("");
                        $("#<%=tbxPictureWidth.ClientID %>").val("");
                        $("#<%=tbxPictureFileSize.ClientID %>").val("");
      
                        $('#imgBox').on('load', function () {
                            var height = this.naturalHeight,
                                width = this.naturalWidth;
                            $("#<%=tbxPictureHeight.ClientID %>").val(height);
                            $("#<%=tbxPictureWidth.ClientID %>").val(width);
                            var imgpath = document.getElementById('theFile');
                            //if (!imgpath.value==""){
                            //var img=imgpath.files\[0\].size;
                            //var imgsize
      
      J Offline
      J Offline
      jkirkerx
      wrote on last edited by
      #2

      To the best of my knowledge, you just listen for the file upload event. In MVC, I wrote a new way of uploading a file, or multiple files at the same time. I used JQuery to listen for the DOM event "change"; file upload button, and ran a JQuery function to intercept the upload and target the data to a JsonResult in one of my controllers, an Ajax controller. Sounds weird huh?. But I will show 2 examples, MVC and Webforms. I believe this is the secret to Uploadify. JQuery: in the document ready, I listened for the upload button to fire an event to upload the file stream, which is just a base64 string representing bytes. So this validates the name of the files, and then runs the function

      $("#AvatarBrowse").on("change", function (e) {
      if (validate_upload_brandAvatar()) {
      run_upload_brandAvatar(e, $(this));
      }
      });

      JQuery: This intercepts the file upload, but passes extra data along with the page event.

      var _extraData = "?id=-1&avatarName=" + _avatarName + "&avatarAlt=" + _avatarAlt;

      if (window.FormData !== undefined) {
      var _data = new FormData();
      for (var x = 0; x < _files.length; x++) {
      _data.append("file" + x, _files[x]);
      }

      $.ajax({
          type: "POST",
          url: '/Ajax/json\_upload\_brand\_avatar' + \_extraData,
          contentType: false,
          processData: false,
          data: \_data,
      

      Then in my Ajax Controller, its an Async Task<> This grabs the file content in the base64 string of bytes, and converts the string to a byte array to save in the database. Then it processes the image, and sends back a base64 string of the image to preview after changes.

      var fileContent = Request.Files[file];
      if (fileContent != null && fileContent.ContentLength > 0)
      {
      // Wait and convert the Stream to a Byte Array
      using (MemoryStream stream_in = new MemoryStream())
      {
      fileContent.InputStream.CopyTo(stream_in);

                 // and optionally write the file to disk
                 string oFileName = fileContent.FileName;
                 string oFileExt = Path.GetExtension(oFileName);
                 string fileName = cleanInput.Clean\_FileName(avatarName.ToLower() + oFileExt);
                 var path = Path.Combine(Server.MapPath("~/Images/avatars/departments"), fileName);
      

      On webforms, it's a different process. you use the IHTTPHandler and ProcessRequest to intercept the file

      1 Reply Last reply
      0
      • T turbosupramk3

        I'm new to ASP, but not c#. The fileupload control seems to be different than the rest of my controls, I cannot seem to link it to my c# code. Do I need to create an event listener for it? Or a background worker process thread to monitor something from it? I'm sure this is caused by me not understanding how the html and c# code work. How can I communicate between the fileupload control and my c sharp code in the background? I tried to put a javascript function into the head portion but it does not appear to be called and when I put a break point in there to test it, it does not hit the break point. Thanks

        <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="True"
        CodeBehind="Default.aspx.cs" Inherits="exchangePictureUpdater.exchangePictureUpdater" %>

        <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

        <script src="jquery-2.2.4.js"></script>
        <script>

              function loadimage() {
                  alert($("#thefile").prop('files'));
              }
        
              function readURL(input) {
                  if (input.files && input.files\[0\]) {
                      var reader = new FileReader();
        
                      reader.onload = function (e) {
                          $('#imgBox')
                              .attr('src', e.target.result)
                          <%--.width(150)
                              .height(200); --%>
                          $("#<%=tbxPictureHeight.ClientID %>").val("");
                          $("#<%=tbxPictureWidth.ClientID %>").val("");
                          $("#<%=tbxPictureFileSize.ClientID %>").val("");
        
                          $('#imgBox').on('load', function () {
                              var height = this.naturalHeight,
                                  width = this.naturalWidth;
                              $("#<%=tbxPictureHeight.ClientID %>").val(height);
                              $("#<%=tbxPictureWidth.ClientID %>").val(width);
                              var imgpath = document.getElementById('theFile');
                              //if (!imgpath.value==""){
                              //var img=imgpath.files\[0\].size;
                              //var imgsize
        
        F Offline
        F Offline
        F ES Sitecore
        wrote on last edited by
        #3

        You don't have any c# code that is interacting with the file upload so you might need to explain your problem better, and maybe be more explicit about the language you use, eg javascript isn't c#, are you trying to interact on the client or the server, and so on.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups