post image base64 to server and save image to disk same fileUpload, that preview in browser
Change submit to call ajax instead:
<input type="submit" value="Send" onclick="SendData(); return false;" />
Add script to handle the sending:
<script type="text/javascript"> function SendData() { var fd = new FormData(); fd.append('file', $('#imageInput')[0].files[0]); $.ajax({ url: '/UploadImage.ashx', data: fd, processData: false, contentType: false, type: 'POST', success: function (data) { alert(data); } }); }</script>
Create .ashx handler that will handle the save:
public class UploadImage : IHttpHandler { public void ProcessRequest(HttpContext context) { var parser = new MultipartParser(context.Request.InputStream); if (parser.Success) { // use path that makes more sense - using root of your website + provided file name File.WriteAllBytes(Path.Combine(context.Server.MapPath("~/"), parser.Filename), parser.FileContents); } } public bool IsReusable { get { return false; } }}
MultipartParser
is opensource class - see http://multipartparser.codeplex.com/SourceControl/latest#MultipartParser.cs
For convenience also attached:
public class MultipartParser { public MultipartParser(Stream stream) { this.Parse(stream, Encoding.UTF8); } public MultipartParser(Stream stream, Encoding encoding) { this.Parse(stream, encoding); } private void Parse(Stream stream, Encoding encoding) { this.Success = false; // Read the stream into a byte array byte[] data = ToByteArray(stream); // Copy to a string for header parsing string content = encoding.GetString(data); // The first line should contain the delimiter int delimiterEndIndex = content.IndexOf("\r\n"); if (delimiterEndIndex > -1) { string delimiter = content.Substring(0, content.IndexOf("\r\n")); // Look for Content-Type Regex re = new Regex(@"(?<=Content\-Type:)(.*?)(?=\r\n\r\n)"); Match contentTypeMatch = re.Match(content); // Look for filename re = new Regex(@"(?<=filename\=\"")(.*?)(?=\"")"); Match filenameMatch = re.Match(content); // Did we find the required values? if (contentTypeMatch.Success && filenameMatch.Success) { // Set properties this.ContentType = contentTypeMatch.Value.Trim(); this.Filename = filenameMatch.Value.Trim(); // Get the start & end indexes of the file contents int startIndex = contentTypeMatch.Index + contentTypeMatch.Length + "\r\n\r\n".Length; byte[] delimiterBytes = encoding.GetBytes("\r\n" + delimiter); int endIndex = IndexOf(data, delimiterBytes, startIndex); int contentLength = endIndex - startIndex; // Extract the file contents from the byte array byte[] fileData = new byte[contentLength]; Buffer.BlockCopy(data, startIndex, fileData, 0, contentLength); this.FileContents = fileData; this.Success = true; } } } private int IndexOf(byte[] searchWithin, byte[] serachFor, int startIndex) { int index = 0; int startPos = Array.IndexOf(searchWithin, serachFor[0], startIndex); if (startPos != -1) { while ((startPos + index) < searchWithin.Length) { if (searchWithin[startPos + index] == serachFor[index]) { index++; if (index == serachFor.Length) { return startPos; } } else { startPos = Array.IndexOf<byte>(searchWithin, serachFor[0], startPos + index); if (startPos == -1) { return -1; } index = 0; } } } return -1; } private byte[] ToByteArray(Stream stream) { byte[] buffer = new byte[32768]; using (MemoryStream ms = new MemoryStream()) { while (true) { int read = stream.Read(buffer, 0, buffer.Length); if (read <= 0) return ms.ToArray(); ms.Write(buffer, 0, read); } } } public bool Success { get; private set; } public string ContentType { get; private set; } public string Filename { get; private set; } public byte[] FileContents { get; private set; }}