.net core使用百度webupload上传图片
后端代码:
/// <summary>
/// 图片上传,上传路径: "/Uploads/Images/" + folder + "/" + saveName
/// </summary>
/// <param name="fileData"></param>
/// <returns></returns>
[HttpPost]
public JsonResult UploadImage([FromServices]IWebHostEnvironment environment,string folder)
{
try
{
var files = Request.Form.Files;
//string contentRootPath = environment.ContentRootPath;//项目所在路径
string webRootPath = environment.WebRootPath;//wwwroot文件夹所在路径
string filePath = webRootPath + "/Uploads/Images/" + folder + "/";
string fullPath = "";
string uploadPath = ""; foreach (var formFile in files)
{
string fileName = Path.GetFileName(formFile.FileName);// 原始文件名称
string fileExtension = Path.GetExtension(fileName).ToLower(); // 文件扩展名
string saveName = DateTime.Now.ToString("yyyyMMddhhmmssffff") + fileExtension; // 保存文件名称
int filesize = int.Parse(formFile.Length.ToString()) / ;//图片大小(KB)
if (filesize > || filesize <= || (fileExtension != ".jpg" && fileExtension != ".png" && fileExtension != ".gif"))
{
return Json(new { Success = false, Message = "上传失败!\r请上传jpg/png/gif格式图片,文件大小不要超过5MB也不要小于2K" });
}
fullPath = Path.Combine(filePath, saveName);
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
using (var stream = new FileStream(fullPath, FileMode.CreateNew))
{
formFile.CopyTo(stream);
}
uploadPath = "/Uploads/Images/" + folder + "/" + saveName;
} return Json(new { Success = true, FilePath = uploadPath });
}
catch (Exception e)
{
NLogHelper.WriteDebug("图片上传方法异常:"+ e.Message.ToString());
return Json(new { Success = false, Message = e.Message.ToString() });
} }
前端代码:
@model Dw.Models.Article.Article_Category
@{
ViewBag.Title = "文章类别管理";
Layout = "~/Views/Shared/_Form.cshtml";
} <script>
var keyValue = '@ViewBag.keyValue';
var parentId = '@ViewBag.parentId';
$(function () {
initControl();
})
//初始化控件
function initControl() {
//上级栏目
$("#ParentId").ComboBoxTree({
url: "/ArticleManage/ArticleCategory/GetTreeJson",
description: "==请选择==",
height: "260px",
allowSearch: true
});
//获取表单
if (!!keyValue) {
$.SetForm({
url: "/ArticleManage/ArticleCategory/GetFormJson",
param: { keyValue: keyValue },
success: function (data) {
$("#form1").SetWebControls(data);
}
});
} else {
$("#ParentId").ComboBoxTreeSetValue(parentId);
}
}
//保存表单
function AcceptClick() {
if (!$('#form1').Validform()) {
return false;
}
var postData = $("#form1").GetWebControls(keyValue);
if (postData["ParentId"] == "") {
postData["ParentId"] = 0;
}
$.SaveForm({
url: "/ArticleManage/ArticleCategory/SaveForm?keyValue=" + keyValue,
param: postData,
loading: "正在保存数据...",
success: function () {
$.currentIframe().$("#gridTable").resetSelection();
$.currentIframe().$("#gridTable").trigger("reloadGrid");
}
})
}
</script>
<div style="margin-left: 10px; margin-top: 20px; margin-right: 30px;">
<table class="form">
@Html.HiddenFor(model => model.CategoryId)
@Html.HiddenFor(model => model.CreateDate)
@Html.HiddenFor(model => model.CreateUserId)
@Html.HiddenFor(model => model.CreateUserName)
<tr>
<th class="formTitle">名称<font face="宋体">*</font></th>
<td class="formValue" colspan="3">
@Html.TextBoxFor(model => model.Name, new { type = "text", onblur = "$.ExistField(this.id,'/ArticleManage/ArticleCategory/ExistName')", @class = "form-control" })
</td>
</tr>
<tr>
<th class="formTitle">上级栏目</th>
<td class="formValue" colspan="3">
<div id="ParentId" type="selectTree" class="ui-select">
</div>
</td>
</tr>
<tr>
<th class="formTitle">导航图片</th>
<td class="formValue" colspan="3">
@*<input type="file" id="picture_upload" name="picture_upload" value='导航图片' />*@
<div id="uploader">
<input type="hidden" value="hid_blog_image"/>
<div id="thelist" class="uploader-list"></div>
@*<a href="javascript:void(0)" Onclick="remove_pic()" id="minus">
<span class="glyphicon glyphicon-remove"></span>
</a>*@
<!--用来存放文件信息-->
<div class="btns">
<div id="picker">选择文件</div>
@*<button id="ctlBtn" class="btn btn-default">开始上传</button>*@
</div>
</div>
</td>
</tr>
<tr>
<th class="formTitle">图片路径</th>
<td class="formValue" colspan="3">
@Html.TextBoxFor(m => m.PictureUrl, new { type = "text", @class = "form-control" })
</td>
</tr>
<tr>
<th class="formTitle">
状态
</th>
<td class="formValue" colspan="3">
<select id="EnabledMark" name="EnabledMark">
<option value="1">有效</option>
<option value="0">无效</option>
</select>
</td>
</tr>
<tr>
<th class="formTitle">
排序
</th>
<td class="formValue" colspan="3">
@Html.TextBoxFor(model => model.SortCode, new { type = "text", @class = "form-control" })
</td>
</tr>
<tr>
<th class="formTitle" valign="top" style="padding-top: 4px;">
备注
</th>
<td class="formValue" colspan="3">
@Html.TextBoxFor(model => model.Description, new { type = "text", @class = "form-control", style = "height: 70px;" })
</td>
</tr>
</table>
</div> <link href="~/scripts/plugins/webuploader/webuploader.css" rel="stylesheet" />
<script src="~/scripts/plugins/webuploader/webuploader.js"></script>
<script type="text/javascript">
var BASE_URL = '';
var uploader = WebUploader.create({
auto: true,
// swf文件路径
swf: BASE_URL + '/scripts/plugins/webuploader/Uploader.swf',
// 文件接收服务端。
server: '/Uploads/UploadImage?folder=ArticleCategory',
// 选择文件的按钮。可选。
// 内部根据当前运行是创建,可能是input元素,也可能是flash.
pick: '#picker',
// 只允许选择图片文件。
accept: {
title: 'Images',
extensions: 'gif,jpg,jpeg,bmp,png',
mimeTypes: 'image/*'
}
}); uploader.on('fileQueued', function (file) {
$list = $('#thelist');
$list.append('<div id="' + file.id + '" class="item">' +
'<h4 class="info">' + file.name + '</h4>' +
'<p class="state">等待上传...</p>' +
'</div>');
}); uploader.on('uploadSuccess', function (file, response) {
var data = eval(response);
if (data.success) {
$('#PictureUrl').val(data.filePath);
}
else {
alert(data.message);
}
$('#' + file.id).find('p.state').text('已上传');
}); uploader.on('uploadError', function (file) {
$('#' + file.id).find('p.state').text('上传出错');
}); uploader.on('uploadComplete', function (file) {
$('#' + file.id).find('.progress').fadeOut();
}); uploader.on('uploadAccept', function (file, response) {
var data = eval(response);
if (data.success) {
$('#thelist').html('<img src="' + data.filePath + '" style="width:200px;margin-bottom:20px" />');
$('#hid_blog_image').val(data.filePath);
$('#minus').show();
}
}); function remove_pic() {
$('#thelist').html('');
$('#hid_blog_image').val('');
$('#minus').hide();
}
</script>
webuploader控件地址: http://fex.baidu.com/webuploader/getting-started.html
.net core使用百度webupload上传图片的更多相关文章
- 百度ueditor上传图片时如何设置默认宽高度
百度ueditor上传图片时如何设置默认宽高度 一.总结 一句话总结:直接css或者js里面限制一下就好,可以用html全局限制一下图片的最大高度 直接css或者js里面限制一下就好,可以用html全 ...
- 百度UEditor上传图片-再总结一次
晚上,在继续开发BriefCMS,把百度UEditor上传图片的问题,给解决了,终于解决了. 公司极简版CMS.BriefCMS.个人官网,最近2个月,与百度UEditor厮杀了好久.最值得吐槽的,就 ...
- 中小研发团队架构实践之生产环境诊断工具WinDbg 三分钟学会.NET微服务之Polly 使用.Net Core+IView+Vue集成上传图片功能 Fiddler原理~知多少? ABP框架(asp.net core 2.X+Vue)模板项目学习之路(一) C#程序中设置全局代理(Global Proxy) WCF 4.0 使用说明 如何在IIS上发布,并能正常访问
中小研发团队架构实践之生产环境诊断工具WinDbg 生产环境偶尔会出现一些异常问题,WinDbg或GDB是解决此类问题的利器.调试工具WinDbg如同医生的听诊器,是系统生病时做问题诊断的逆向分析工具 ...
- angularjs + fis +modJS 对于支持amd规范的组建处理(PhotoSwipe 支持,百度webUpload支持)
这不是很好的处理方式,但是能够解决问题,希望有大神推荐更好的方式. 前端模块使用angularjs + fis +modJS 开发前端应用有两个月了.总结了以下的优点: fis 自动构建,自动发布,功 ...
- 使用.Net Core+IView+Vue集成上传图片功能
最近的项目里有上传图片的功能,当然这个功能在项目里是必须要有的,那么目前这个项目是使用完全的前后端分离,在选择文件上传的组件中还是选择了全面支持Vue的IView,任何上传图片都是通过HTTP请求,服 ...
- 使用Webupload上传图片到FastDFS分布式文件系统
使用Webupload插件上传图片到FastDFS分布式文件系统. 前提条件:1.已安装FastDFS分布式文件系统 2.使用webuploader插件上传文件 3.maven工程已引入FastDFS ...
- .net core 2.0 webuploader上传图片
引入文件 <link href="~/Scripts/webuploader-0.1.5/webuploader.css" rel="stylesheet" ...
- 百度ueditor 上传图片后如何设置样式
最近项目中遇到一个问题,UEditor上传图片后,在内容展示会修改图片样式.但是表情也是img标签,所以全局修改是有问题的, 所以只能着手修改一下插件的代码. 首先找到图片上传的服务器段文件.这里主要 ...
- 关于 百度 Ueditor 上传图片时 打开文件夹的延迟问题
在使用 ueditor 开发时, 作为一个web文本编辑器使用时. 当点击上传图片时, 文件夹要延迟好久才能打开. 解决: 针对多图片上传, 将/ueditor/dialogs/image/image ...
随机推荐
- JavaScript动态加载script方式引用百度地图API 拓展---JavaScript的Promise
上一篇博客JavaScript动态加载script方式引用百度地图API,Uncaught ReferenceError: BMap is not defined 这篇文章中我接触到一个新的单词:Pr ...
- python while语句
一.while 1.while 死循环 f=True while f: print(1) print(2) 2.while 活循环 ①.正序 count = 1 while count <= 5 ...
- Linux:搭建samba服务器
samba服务器的搭建 修改防火墙设置 firewall-cmd --permanent --add-service=samba //设置防火墙 firewall-cmd --reload //重新加 ...
- ifconfig|grep eth0|awk '{print $5}' 命令详解
因需要将linx下获取某个网中的MAC地址,可以使用如下命令获取: ifconfig|grep eth0|awk '{print $5}' ifconfig: 输出linux下所有网口的信息(包括IP ...
- 05-Django后台管理和视图
Django的后台管理可以方便的生成管理页面,使用前先准备如下: 1.本地化 语言和时区的本地化,修改settings.py文件 # LANGUAGE_CODE = 'en-us' LANGUAGE_ ...
- ORA-29861: 域索引标记为 LOADING/FAILED/UNUSABLE
解决方法:select idx_name,idx_status from ctxsys.ctx_indexes;需要重建同步全文索引:alter index 索引名 rebuild online ...
- 不依赖官方LibPack编译FreeCAD的一次尝试
在Windows下编译FreeCAD,通常的方法是依赖官方提供的LibPack,但是只有vs2008, vs2012, vs2013等几个版本提供.比如现在感觉vs2017比较好用,可是没有官方Lib ...
- 8. Vue - Router
一.Vue Router 的使用 JavaScript: 1.创建组件:创建单页面应用需要渲染的组件 2.创建路由:创建VueRouter实例 3.映射路由:调用VueRouter实例的map方法 4 ...
- 第05组 Beta冲刺(3/4)
第05组 Beta冲刺(3/4) 队名:天码行空 组长博客连接 作业博客连接 团队燃尽图(共享): GitHub当日代码/文档签入记录展示(共享): 组员情况: 组员1:卢欢(组长) 过去两天完成了哪 ...
- Python 十大装 X 语法(二)
Python 是一种代表简单思想的语言,其语法相对简单,很容易上手.不过,如果就此小视 Python 语法的精妙和深邃,那就大错特错了.本文精心筛选了最能展现 Python 语法之精妙的十个知识点,并 ...