Transmit the image in json format (base64 encoding ),
Procedure 1:
Background code:
Public ActionResult Index () {FileStream fs = new FileStream ("e: \ file \ psb.jpg", FileMode. open); byte [] t = StreamToBytes (fs); ViewBag. str = Convert. toBase64String (t); return View ();} public byte [] StreamToBytes (Stream stream) {byte [] bytes = new byte [Stream. length]; stream. read (bytes, 0, bytes. length); // set the current stream position to the starting stream of the stream. seek (0, SeekOrigin. begin); return bytes ;}
Front-end code:
@ {ViewBag. Title = "Index" ;}< script src = "~ /Gc-ui/js/gc-js/jq_print/jquery-1.4.4.min.js "> </script> <script src = "~ /Gc-ui/js/gc-js/jq_json/jquery. json-2.4.min.js "> </script> <script> $ (function () {var json = {" MATERIALNAME ": $ (" # MATERIALNAME "). val (), "REMARK": $ ("# REMARK "). val (), "IDRNTITYID": $ ("# IDRNTITYID "). val (), "AFFAIRID": $ ("# AFFAIRID "). val (), "BinaryImg": $ ("# BinaryImg "). val ()} $ ("# xc "). val ($. toJSON (json); $ ("form "). submit (function () {alert ();});}); </script> <form method = "post" action = "xxx"> <input id = "MATERIALNAME" value = "sb"/> <input id = "REMARK" value = "sb"/> <input id = "IDRNTITYID" value = "70"/> <input id = "AFFAIRID" value = "511"/> <input id = "BinaryImg" value = "@ ViewBag. str "/> <input id =" xc "name =" json "type =" hidden "value =" "/>
<Button> test <button> </form>
Procedure 2:
Background code:
// String streaming
// M_l.BinaryImg base64 encoded image file stream
// Note that post is required for a request
Byte [] bt = Convert. fromBase64String (m_l.BinaryImg); System. IO. memoryStream stream = new System. IO. memoryStream (bt); // File Format string fileName = DateTime. now. toString ("yyyyMMddHHmmssfff") + ". jpg "; string dicr = DateTime. now. toString ("yyyy-MM-dd") + "/"; string RetureVlue = dicr + fileName; string physicsPath = System. configuration. configurationManager. deleetask[ "FilePath"]; if (! Directory. Exists (physicsPath + dicr) {Directory. CreateDirectory (physicsPath + dicr);} StreamToFile (stream, physicsPath + RetureVlue );
public static void StreamToFile(Stream stream, string filepath) { byte[] bytes = StreamToBytes(stream); FileStream fileStream = new FileStream(filepath, FileMode.Create); fileStream.Write(bytes, 0, bytes.Length); fileStream.Flush(); fileStream.Close(); }
The function implemented in this article is to pass images in json format and save the Received images.
I originally wanted to compress the string. However, a java and. net Algorithm for compression and decompression is not found ..
Please give me some advice...
jquery.json-2.4.min.js
/*! jQuery JSON plugin 2.4.0 | code.google.com/p/jquery-json */(function ($) { 'use strict'; var escape = /["\\\x00-\x1f\x7f-\x9f]/g, meta = { '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"': '\\"', '\\': '\\\\' }, hasOwn = Object.prototype.hasOwnProperty; $.toJSON = typeof JSON === 'object' && JSON.stringify ? JSON.stringify : function (o) { if (o === null) { return 'null'; } var pairs, k, name, val, type = $.type(o); if (type === 'undefined') { return undefined; } if (type === 'number' || type === 'boolean') { return String(o); } if (type === 'string') { return $.quoteString(o); } if (typeof o.toJSON === 'function') { return $.toJSON(o.toJSON()); } if (type === 'date') { var month = o.getUTCMonth() + 1, day = o.getUTCDate(), year = o.getUTCFullYear(), hours = o.getUTCHours(), minutes = o.getUTCMinutes(), seconds = o.getUTCSeconds(), milli = o.getUTCMilliseconds(); if (month < 10) { month = '0' + month; } if (day < 10) { day = '0' + day; } if (hours < 10) { hours = '0' + hours; } if (minutes < 10) { minutes = '0' + minutes; } if (seconds < 10) { seconds = '0' + seconds; } if (milli < 100) { milli = '0' + milli; } if (milli < 10) { milli = '0' + milli; } return '"' + year + '-' + month + '-' + day + 'T' + hours + ':' + minutes + ':' + seconds + '.' + milli + 'Z"'; } pairs = []; if ($.isArray(o)) { for (k = 0; k < o.length; k++) { pairs.push($.toJSON(o[k]) || 'null'); } return '[' + pairs.join(',') + ']'; } if (typeof o === 'object') { for (k in o) { if (hasOwn.call(o, k)) { type = typeof k; if (type === 'number') { name = '"' + k + '"'; } else if (type === 'string') { name = $.quoteString(k); } else { continue; } type = typeof o[k]; if (type !== 'function' && type !== 'undefined') { val = $.toJSON(o[k]); pairs.push(name + ':' + val); } } } return '{' + pairs.join(',') + '}'; } }; $.evalJSON = typeof JSON === 'object' && JSON.parse ? JSON.parse : function (str) { return eval('(' + str + ')'); }; $.secureEvalJSON = typeof JSON === 'object' && JSON.parse ? JSON.parse : function (str) { var filtered = str.replace(/\\["\\\/bfnrtu]/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''); if (/^[\],:{}\s]*$/.test(filtered)) { return eval('(' + str + ')'); } throw new SyntaxError('Error parsing JSON, source is not valid.'); }; $.quoteString = function (str) { if (str.match(escape)) { return '"' + str.replace(escape, function (a) { var c = meta[a]; if (typeof c === 'string') { return c; } c = a.charCodeAt(); return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); }) + '"'; } return '"' + str + '"'; };}(jQuery));