Wednesday, October 29, 2014

Get httpPostedFileBase FileName only code or extract last portion of file director path

var httpPostedFileBase = Request.Files.Get("DocumentFileName");
               var myFileName = string.Empty;
               if (httpPostedFileBase != null)
               {
                   int position = httpPostedFileBase.FileName.LastIndexOf('\\');
                   myFileName = httpPostedFileBase.FileName.Substring(position + 1);
               }

Friday, October 24, 2014

Wednesday, October 15, 2014

Reading Keys from Web.config file.

Try using the WebConfigurationManager class instead. For example:
C# string userName = WebConfigurationManager.AppSettings["PFUserName"];
OR
VB.NET Dim userName as String = WebConfigurationManager.AppSettings("PFUserName")

Read file using FileStream - Reference - http://www.csharp-examples.net/filestream-read-file/

Read file using FileStream

First create FileStream to open a file for reading. Then call FileStream.Read in a loop until the whole file is read. Finally close the stream.
[C#]
using System.IO;

public static byte[] ReadFile(string filePath)
{
  byte[] buffer;
  FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  try
  {
    int length = (int)fileStream.Length;  // get file length
    buffer = new byte[length];            // create buffer
    int count;                            // actual number of bytes read
    int sum = 0;                          // total number of bytes read

    // read until Read method returns 0 (end of the stream has been reached)
    while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
      sum += count;  // sum is a buffer offset for next reading
  }
  finally
  {
    fileStream.Close();
  }
  return buffer;
}

Wednesday, October 01, 2014

jQuery $.ajax call add functions you will need to call after the html is rendered.

I am adding the following blog post because I spent a good amount of time working on figuring the following out.

Using the jQuery function $.ajax I learned that you need to be sure to add all the functions you want to perform actions on after the $.ajax loads the new content to the page.

CORRECT CODE
<script type="text/javascript">
    $("#addItem").click(function () {
        $.ajax({
            url: this.href,
            cache: false,
            success: function (html) {
                $("#editorRows").append(html);
                $("a.deleteRow").on("click", function () {
                    $(this).parents("div.editorRow:first").remove();
                    return false;
                });
            }
        });
        return false;
    });

</script>

This makes 100% sense because if you code it as follows it will never work because the .append(html) is not on the page currently when the jQuery loads.

BAD CODE
<script type="text/javascript">
    $("#addItem").click(function () {
        $.ajax({
            url: this.href,
            cache: false,
            success: function (html) {
                $("#editorRows").append(html);
            }
        });
        return false;
    });

//Following will not be called --- because it is not on the page currently
    $("a.deleteRow").on("click", function () {
        $(this).parents("div.editorRow:first").remove();
        return false;
    });
</script>


Hope this helps.  Have fun coding.