Saturday, June 25, 2011

Asp.net with Uploadify Sample Project. VB

 Download Source code. 

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="PictureUpload._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
 
    <script src="scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
    <script src="scripts/jquery.uploadify.v2.1.4.js" type="text/javascript"></script>
    <script src="scripts/swfobject.js" type="text/javascript"></script>
    <link href="css/uploadify.css" rel="stylesheet" type="text/css" />
    <script type = "text/javascript">
     $(window).load(

    function () {

        $("#<%=FileUpload1.ClientID %>").uploadify({

            'uploader': 'scripts/uploadify.swf',

            'cancelImg': 'images/cancel.png',

            'buttonText': 'Browse Files',

            'script': 'UploadVB.ashx',

            'folder': 'uploads',

            'fileDesc': 'Image Files',

            'fileExt': '*.jpg;*.jpeg;*.gif;*.png',

            'multi': true,

            'auto': true

        });

    }

);

</script> 

</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div style = "padding:40px">

        <asp:FileUpload ID="FileUpload1" runat="server" />
     
    </div>
    </div>
    </form>
    <p>
       </p>
</body>
</html>


The upload handler

Imports System.Web
Imports System.Web.Services
Imports System

Imports System.IO
Public Class UploadVB
    Implements System.Web.IHttpHandler

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        Dim postedFile As HttpPostedFile = context.Request.Files("Filedata")



        Dim savepath As String = ""

        Dim tempPath As String = ""

        tempPath = System.Configuration.ConfigurationManager.AppSettings("FolderPath")

        savepath = context.Server.MapPath(tempPath)

        Dim filename As String = postedFile.FileName

        If Not Directory.Exists(savepath) Then

            Directory.CreateDirectory(savepath)

        End If



        postedFile.SaveAs((savepath & "\") + filename)

        context.Response.Write((tempPath & "/") + filename)

        context.Response.StatusCode = 200

    End Sub



    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable

        Get

            Return False

        End Get

    End Property

End Class






Tuesday, April 19, 2011

Search Bing Through Silverlight C#

Search Bing with silverlight.   Download The Code

First step. Head over to http://www.bing.com/developers/ to get your free api key. It only takes a second and the app will not work without it.
This is for the API version 2.0.  The latest version as of this writing.  If you try other samples out there calling the old service api you will just end  up pulling your hair out like I did.   Microsoft is not issuing any new keys for the old live api search so most of the code examples on the web are worthless.

I'm short of time so I am just going to post the working project and briefly go over the concepts. You will have access to the documentation once you get your key.  BEWARE. Much of the documentation will not work with silverlight.  The examples I saw from Microsoft were JSON or REST.  They forgot to go over the best method, SOAP.  This project uses SOAP to simplify things.

General concept is as follows.
Create a silverlight with asp.net application.

Create a service reference to the Bing API.  http://api.bing.net/search.wsdl
Add a textbox, a button and a datagrid.  The textbox will hold the search term, the button calls the service and the grid displays the results.

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CSSearchBing.BingSearch; //This is the service to bing.
namespace CSSearchBing
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
//construct the call
            BingSearch.SearchRequest request = new BingSearch.SearchRequest();
 // Enter you api key.   Should be put in config file for security reasons. 
          request.AppId = "CC37A72C372AA66EA3BEB42292DDDDDDDDD";

          request.Version="2.2";
          request.Adult = AdultOption.Off;
          request.AdultSpecified = true;
          request.Query = txtSearch.Text; // search for what is in the textbox
          request.Sources = new BingSearch.SourceType[] { BingSearch.SourceType.Web };
          request.Web =new BingSearch.WebRequest();
         request.Web.Count = 10; //Ten results.  Read the documentation on how to get more.
          request.Web.CountSpecified = true;
          BingSearch.BingPortTypeClient client = new BingSearch.BingPortTypeClient("BingPort"); // BingPort is needed as is. Endpoint
          client.SearchCompleted += new EventHandler<SearchCompletedEventArgs>(client_SearchCompleted);
          client.SearchAsync(request);
        }
// our search complete handler.  Required for silverlight.
        void client_SearchCompleted(object sender, SearchCompletedEventArgs e)
        {
            SearchResponse response = e.Result;
            if (response.Web.Results.Count() > 0)
            {
                var results = from result in response.Web.Results
                              select new SearchResult
                              {
                                  SearchResultTitle = result.Title,
                                  SearchResultUri = result.Url,
                                  SearchResultDescription = result.Description
                              };

                dataGrid1.ItemsSource = results.ToList();
            }
        }
// class holds result values to pass on
        public class SearchResult
        {
            public string SearchResultTitle { get; set; }
            public string SearchResultDescription { get; set; }
            public string SearchResultUri { get; set; }
        }
  
  
    }
  
    }