通过json传递图片(base64编码)
程序一:
后台代码:
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); // 设置当前流的位置为流的开始 stream.Seek(0, SeekOrigin.Begin); return bytes; }
前台代码:
@{ ViewBag.Title = "Index"; } <script src="~/gc-ui/js/gc-js/gc-js/jq_print/jquery-1.4.4.min.js"></script> <script src="~/gc-ui/js/gc-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>测试<button> </form>
程序二:
后台代码:
//字符串转流
//m_l.BinaryImg程序一传过来的base64编码的图片文件流
//注意程序一请求要用post
byte[] bt = Convert.FromBase64String(m_l.BinaryImg); System.IO.MemoryStream stream = new System.IO.MemoryStream(bt); //文件格式 string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg"; string dicr = DateTime.Now.ToString("yyyy-MM-dd") + "/"; string RetureVlue = dicr + fileName; string physicsPath = System.Configuration.ConfigurationManager.AppSettings["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(); }
本文实现的功能是通过json传递图片,并把接收到的图片保存下来。
我原本是想压缩一下字符串的。不过没找到一种java和.net通用的压缩和解压缩的算法。。
求大神指点。。。
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));
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)