ASP.NET webservice for sparkbooth

Chad Michael's Avatar

Chad Michael

25 May, 2013 09:01 PM

Just whipped this up as I'm trialing your product. Thought you might want to make it available as a sample on your site like you have witht he php one.

Requires .NET 4.0 framework

In Visual Studio, Create a new Blank Web Application, Add a webservice and past into the code.

  1. 1 Posted by Chad Michael on 25 May, 2013 09:05 PM

    Chad Michael's Avatar

    Oh, and BTW: then configure your spark booth for Custom Upload and put in http://localhost:<testport#>/WebService1.asmx/HelloWorld as the ServiceURL (where <testport#> is whatever the webdev server is set to for your session). Of course in a production environment, it would be port 80 by default.

  2. Support Staff 2 Posted by John on 25 May, 2013 09:19 PM

    John's Avatar

    Thanks Chad. You are the man! :)

  3. Support Staff 3 Posted by Joe Connell on 25 May, 2013 09:29 PM

    Joe Connell's Avatar

    Here is the code: Add a webservice and paste into the code.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web.Services;
    
    namespace WebApplication1
    {
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class WebService1 : WebService
    {
    
        [WebMethod]
        public void HelloWorld()
        {
            string errorMessage = "";
            string response = "";
            string rootPath = "C:\\Temp\\";
            string fileName = Context.Request.Files["media"].FileName;
    
            if (Context.Request.Files["media"].ContentLength > 1350000)
                errorMessage = "Your file is too large.";
    
            if (Context.Request.Files["media"].ContentType != "application/octet-stream")
                errorMessage = String.Format("Invalid file type, file type = {0}", Context.Request.Files["media"].ContentType);
    
            try
            {
                fileName = NextUniqueFilename(fileName, f => File.Exists(Path.Combine(rootPath, f)));
            }
            catch (Exception e)
            {
                errorMessage = e.Message;
            }
    
            if (errorMessage == "")
            {
                Stream sparkStream = Context.Request.Files["media"].InputStream;
                FileStream fileStream = File.Create(String.Format("C:\\Temp\\{0}", fileName));
                sparkStream.CopyTo(fileStream);
                fileStream.Close();
            }
    
            if (errorMessage == "")
                response = "<?xml version='1.0' encoding='UTF-8'?><rsp status='ok' />";
            else
                response = String.Format("<?xml version='1.0' encoding='UTF-8'?><rsp status='fail'><err msg='{0}' /></rsp>", errorMessage);
    
            this.Context.Response.Write(response);
        }
    
        /// <summary>
        /// Finds the next unused unique (numbered) filename.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="inUse">Function that will determine if the name is already in use</param>
        /// <returns>The original filename if it wasn't already used, or the filename with " (n)"
        /// added to the name if the original filename is already in use.</returns>
        private string NextUniqueFilename(string fileName, Func<string, bool> inUse)
        {
            if (!inUse(fileName))
            {
                // this filename has not been seen before, return it unmodified
                return fileName;
            }
            // this filename is already in use, add " (n)" to the end
            var name = Path.GetFileNameWithoutExtension(fileName);
            var extension = Path.GetExtension(fileName);
            if (name == null)
            {
                throw new Exception("File name without extension returned null.");
            }
            const int max = 9999;
            for (var i = 1; i < max; i++)
            {
                var nextUniqueFilename = string.Format("{0} ({1}){2}", name, i, extension);
                if (!inUse(nextUniqueFilename))
                {
                    return nextUniqueFilename;
                }
            }
            throw new Exception(string.Format("Too many files by this name. Limit: {0}", max));
        }
    
    }
    }
    

    . . . sorry I didn't understand
    Joe Connell - SB Support

  4. John closed this discussion on 04 Jun, 2013 06:46 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac