HSS Interlink 2.0 - Quick Start

NOTE: The documentation below is for v2.0, for v3.0 and examples demonstrating the newest features, please see the demo projects included in the source code

  1. This assumes Visual Studio 2010 and .NET 4.0, and running in debug mode from the IDE.
  2. Download and install HSS Interlink
  3. Create a new Silverlight Application with the following name "SLFileUploadTest" (hosted with a Web Site Application "SLFileUploadTest.Web").
  4. Add a reference to HSS.Interlink.Web for the Web Site App
  5. Add a reference to HSS.Interlink to the SL App
  6. Add a reference to System.Windows.Control to the SL App
  • From the SL App, on the main page, add a new button control.
<Button Content="Browse" Height="24" Width="75" Click="Button_Click" />
  • Then in the OnClick event, paste the following
private void Button_Click(object sender, RoutedEventArgs e)
{
    var udialog = new HSS.Interlink.UploadFileDialog();
    udialog.MaxFileSizeKB = int.MaxValue;
    udialog.AllowFileOverwrite = true;
    udialog.BrowseAndShow();
}
  • Then from the Web Site App, create a new class called UploadHandler, and paste the following
using System.Diagnostics;
using System.IO;
using HSS.Interlink.Web;

namespace SLFileUploadTest.Web
{ public class UploadHandler : HSS.Interlink.Web.BaseUploadHandler { /// <summary> /// The Folder where the uploaded files are stored. /// </summary> public static string FileStoreFolder = @"Interlink\Uploads"; // If you support retries //string retryKey; public UploadHandler() { // // Uncomment the following line to turn off purging of temp files. // // NOTE: // The default value for purging is 5 minutes. // If your uploads take longer than 5 minutes // you WILL have to modify this to be longer // than your anticipated longest individual // file upload duration. // //this.PurgeInterval = 0; } string GetFilePath() { return Path.Combine(this.GetFolder(FileStoreFolder), this.FileName); } #region BaseUploadHandler Members public override bool CheckFileExists() { //Debug.WriteLine("CheckFileExists..."); // Take some action based upon the metadata. //Debug.WriteLine("Metadata: " + this.Metadata); string file = GetFilePath(); return File.Exists(file); // If you support overwriting files, do so in CreateNewFile. } public override Responses CreateNewFile() { #region Test Retry (see AppendToFile) // If you support for retries //this.retryKey = this.Query.JobId + this.FileName + "retries"; //this.Context.Cache.Add(retryKey, 0, null, DateTime.Now.AddMinutes(60), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null); #endregion // Take some action based upon the metadata. //Debug.WriteLine("Metadata: " + this.Metadata); string file = this.GetFilePath(); try { if (File.Exists(file))// If you support overwriting files, do so here. File.Delete(file); } catch { } File.Create(file).Close(); return HSS.Interlink.Web.Responses.Success; } public override Responses AppendToFile(byte[] buffer) { //System.Threading.Thread.Sleep(1200); //Debug.WriteLine("Appending..."); // Take some action based upon the metadata. //Debug.WriteLine("Metadata: " + this.Metadata); #region Test exception //throw new Exception("test"); #endregion #region Test Retry (see CreateNewFile) // If you support for retries // If not able to persist chunk, request a Retry... //if (someTest == Failed) //{ // int retries = 0; // this.retryKey = this.Query.JobId + this.FileName + "retries"; // object objRetries = this.Context.Cache[retryKey]; // if (null == objRetries) // retries = 1; // else // retries = (int)objRetries + 1; // this.Context.Cache[retryKey] = retries; // if (retries > 3) // return HSS.Interlink.Web.Responses.FatalError; // return HSS.Interlink.Web.Responses.AppendFileRetry; //} #endregion string file = this.GetFilePath(); using (FileStream fs = File.Open(file, FileMode.Append)) fs.Write(buffer, 0, buffer.Length); return HSS.Interlink.Web.Responses.Success; } public override void CancelUpload() { string file = this.GetFilePath(); try { if (File.Exists(file)) File.Delete(file); } catch { } } public override string UploadComplete() { string file = GetFilePath(); // Move to final destination. //File.Copy(file, finalFile); // Delete //File.Delete(file); //Debug.WriteLine("Completed..."); return "Hey we made it!"; // Optional string to return to caller. } public override bool IsAuthorized() { //Debug.WriteLine("IsAuthorized..."); #region Test Authorization //if (this.Context.User.Identity.IsAuthenticated) //{ // if (this.Context.User.IsInRole("requiredRole")) // return true; //} //return false; #endregion return true; } public override void OnError(System.Exception ex) { // LogFile.Write(ex.ToString()); string file = this.GetFilePath(); try { if (File.Exists(file)) File.Delete(file); } catch { } } #endregion } }
  • And finally in your web config, paste the following
<configuration>
  <appSettings>
    <!-- This value must match the Full TypeName including the namespace of your UploadHandler -->
    <add key="UploadHandler" value="SLFileUploadTest.Web.UploadHandler, SLFileUploadTest.Web"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpHandlers>
      <add verb="GET,POST" path="FileUpload.ashx" type="HSS.Interlink.Web.FileUpload, HSS.Interlink.Web" />
    </httpHandlers>
  </system.web>
</configuration>
  • Hit F5, and you should be able to upload files...

Last edited Jun 15, 2012 at 5:16 AM by hyspdrt, version 18

Comments

dega Oct 19, 2012 at 5:01 AM 
Hi
.UploadFileDialog(); method is not recognized from this example. Is it changed in Version 3?

Asskicker Dec 10, 2011 at 1:15 PM 
Where do I get the FileUpload.ashx file from?!

Solidilos Feb 14, 2011 at 7:18 AM 
Well done! Great step by step introductory tutorial.

Thanks hyspdrt!