使用base64转码的方式上传图片
1.前端html代码
- <input style="width:100%" onchange="loadpicture(1)" type="file" name="file" id="file➩1">
- <img id="pic➩1" style="width:100%;height:120px" src="/Resource/Document/NetworkDisk/ProjectActivity/590600b4-f330-43c2-96fb-ea221f9e0ce2.jpg">
- <input hidden="true" id="picsrc➩1" class="txt">
- <input hidden="true" id="PictureUrl➩1" class="txt">
2.js代码,使用FileReader读取文件
- function loadpicture(i) {
- var obj = document.getElementById("file➩" + i);
- var file = obj.files[0];
- //alert(file);
- //创建读取文件的对象
- var reader = new FileReader();
- //创建文件读取相关的变量
- var imgFile;
- //为文件读取成功设置事件
- reader.onload = function (e) {
- //alert('文件读取完成');
- imgFile = e.target.result;
- //console.log(imgFile);
- $("#pic➩" + i).attr('src', imgFile);
- $("#picsrc➩" + i).val(imgFile);
- };
- //正式读取文件
- reader.readAsDataURL(file);
- }
3.ajax传后台,此处有封装,用GetTableDataJson的方式获取了每一个file的ID
实际传输的话只需要传
- $("#picsrc➩" + i).val()的值
- //保存事件
- function AcceptClick() {
- if (!CheckDataValid('#form1')) {
- return false;
- }
- var ActivityForm = GetTableDataJson("#ActivityForm");
- var ProblemForm = GetTableDataJson("#ProblemForm");
- Loading(true, "正在提交数据...");
- window.setTimeout(function () {
- var postData = GetWebControls("#form1");
- postData["ActivityForm"] = ActivityForm;
- postData["ProblemForm"] = ProblemForm;
- postData["BuildFormJson"] = JSON.stringify(GetWebControls("#CustomAttribute"));
- AjaxJson("/ProjectManageModule/project/SubmitActivityForm?KeyValue=" + GetQuery('KeyValue'), postData, function (data) {
- tipDialog(data.Message, 3, data.Code);
- top.frames[tabiframeId()].windowload();
- closeDialog();
- });
- }, 200);
- }
4.后台c#代码,有可能会报GDI一般性错误的报错,
我这里是一开始目录没有对,所以报错,
- realPath可以用
- this.Server.MapPath(VirtualPath)的方式获取
由于我这边的文件存储目录和应用目录不在同一个盘符,所以使用了直接路径的写法
- #region 图片处理,采用base64的方式转码解码
- string virtualPath = "";
- //图片上传
- if (!string.IsNullOrEmpty(entityD.picsrc))
- {
- //删除老的图片
- string FilePath = this.Server.MapPath(entityD.PictureUrl);
- if (System.IO.File.Exists(FilePath))
- System.IO.File.Delete(FilePath);
- string fileGuid = CommonHelper.GetGuid;
- //long filesize = Filedata.ContentLength;
- string FileEextension = ".jpg";
- string uploadDate = DateTime.Now.ToString("yyyyMMdd");
- //string UserId = ManageProvider.Provider.Current().UserId;
- virtualPath = string.Format("~/Resource/Document/NetworkDisk/{0}/{1}{2}", "ProjectActivity", fileGuid, FileEextension);
- string realPath = string.Format(@"D:\LeaRun\Resource\Document\NetworkDisk\{0}\{1}{2}", "ProjectActivity", fileGuid, FileEextension);
- //string fullFileName = this.Server.MapPath(virtualPath);
- ////创建文件夹,保存文件
- //realPath = Path.GetDirectoryName(fullFileName);
- //先处理图片文件
- string temp = entityD.picsrc.Substring();
- byte[] arr2 = Convert.FromBase64String(entityD.picsrc.Substring());
- using (MemoryStream ms2 = new MemoryStream(arr2))
- {
- System.Drawing.Bitmap bmp2 = new System.Drawing.Bitmap(ms2);
- bmp2.Save(realPath, System.Drawing.Imaging.ImageFormat.Jpeg);
- bmp2.Dispose();
- ms2.Close();
- }
- entityD.PictureUrl = virtualPath;
- }
- #endregion
使用base64转码的方式上传图片的更多相关文章
- 前端以BASE64码的形式上传图片
前端以BASE64码的形式上传图片 一直有一个很苦恼的问题困扰着铁柱兄,每次上传图片的时候前端要写一大堆js,然后后台也要写一堆java代码做处理.于是就在想,有没有简单又方便的方法把图片上传.今天算 ...
- 直接用<img> 的src属性显示base64转码后的字符串成图片
直接用<img> 的src属性显示base64转码后的字符串成图片 <img src="base64转码后的字符串" ></img> 下面的图片 ...
- javascript 使用btoa和atob来进行Base64转码和解码
javascript原生的api本来就支持,Base64,但是由于之前的javascript局限性,导致Base64基本中看不中用.当前html5标准正式化之际,Base64将有较大的转型空间,对于H ...
- Base64的编码转换方式
下面,详细介绍Base64的编码转换方式. 所谓Base64,就是说选出64个字符----小写字母a-z.大写字母A-Z.数字0-9.符号"+"."/"(再加上 ...
- Eclipse Class Decompiler影响class默认打开方式,重新设置Eclipse默认源码打开方式
安装Eclipse Class Decompiler插件后,Eclipse中的默认源码打开方式被修改为Eclipse Class Decompiler 这不是我喜欢的,因为我希望,源码从网络中获取,当 ...
- 控件真的很好用,突然感觉自己以前研究Discuz!NT366源码的方式很2了
控件真的很好用,突然感觉自己以前研究Discuz!NT366源码的方式很2了,就是按钮上的或其他控件上的图片哪里去了?
- JS图片上传后base64转码
代码: // 获取文件流 var fileObj = document.getElementById('inputId').files; // 实例化一个FileReader对象 var reader ...
- Python Base64转码解码
Python Base64 提供了好几种方法例如: encode, decode, encodestring, decodestring, b64encode, b64decode, standard ...
- javascript Base64转码解码
javascript 使用btoa和atob来进行Base64转码和解码 $scope.checkAddCookie = function() { var expireDate = new Date( ...
随机推荐
- 64位windows+32位JDK8+32位eclipse是可以的
- 真机IOS8.3以上的文件夹共享
ios8.3以上的版本,苹果规定需要验证身份,将不在默认开启文件共享,但是在实际测试工作中,提取文件是经常需要做的操作,笔者在使用GT采集性能数据后,通过itoos或itunes都无法获得目标app的 ...
- day3:vcp考试
Q41. An administrator creates a custom ESXi firewall rule using an XML file, however the rules do no ...
- [leetcode]283. Move Zeroes移零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- 将php数据下载csv文件
<?php $sales = array( array( 'Northeast', '2005-01-01', '2005-02-01', 12.54 ), array( 'Northwest' ...
- bzr: ERROR: These branches have diverged. Use the missing command to see how.
这个错误是在提交之后执行bzr pull时出现的,先uncommit,再pull就可以了.
- illumina SBS测序详解
illumina SBS测序详解 2018年01月02日 09:33:56 sixu_9days 阅读数:9789 标签: 生物信息学二代测序 更多 个人分类: 测序原理 最近回头重新看了illl ...
- [Git]Git的常用命令
Update: git status git diff wq git commit -am "why update files" git push Add: git add . g ...
- cxf soap rest webservice spring
1. 导入 jar 包 2. 编写接口 3. 编写实现 4. 配置spring 配置文件 5. 配置web.xml servlet 6. 访问 package com.diancai.test; im ...
- 多线程的使用:只能用cmd来玩不能用idle
from multiprocessing import Process import os def pro_do(name, func): print () if __name__ == " ...