.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 ...
随机推荐
- iPhone每一代的屏幕尺寸比例是多少?
参考链接: https://www.jianshu.com/p/8f566ce3bc2c https://zhidao.baidu.com/question/1668756575750668747.h ...
- JS Switch
JS Switch switch 语句用于基于不同的条件来执行不同的动作. switch(n) { case 1: 执行代码块 1 break; case 2: 执行代码块 2 break; defa ...
- springboot架构下运用shiro后在configuration,通过@Value获取不到值,总是为null
通过网上查找资料,是因为shiro的bean @Beanpublic LifecycleBeanPostProcessor lifecycleBeanPostProcessor() { return ...
- Rewrite基本概述
Rewrite基本概述 什么是rewrite Rewrite主要实现url地址重写,以及重定向,就是把传入web的请求重定向到其他url的过程. rewrite使用场景 1.地址跳转,用户访问www. ...
- 利用phpqrcode二维码生成类库合成带logo的二维码并且用合成的二维码生成海报(二)
前期准备 引入phpqrcode类库(下载地址:https://download.csdn.net/download/weixin_37557729/11891240:支持彩色二维码的下载地址:htt ...
- ubuntu 安装精简桌面; VNC; vncserver 配置
安装最简单的环境: apt-get install gnome-shell apt-get install gnome-panel apt-get install gnome-menus ...
- 201871010116-祁英红《面向对象程序设计(java)》第十一周学习总结
博文正文开头格式:(2分) 项目 内容 <面向对象程序设计(java)> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://ww ...
- poj 1979 Red and Black 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...
- 洛谷 P5640 【CSGRound2】逐梦者的初心
洛谷 P5640 [CSGRound2]逐梦者的初心 洛谷传送门 题目背景 注意:本题时限修改至250ms,并且数据进行大幅度加强.本题强制开启O2优化,并且不再重测,请大家自己重新提交. 由于Y校的 ...
- for(var i in items) 和 for(var i;i<items.length;i++) 区别
前者循环的是属性,后者循环的才是数组. 若项目中对数组属性进行了扩展,那切记不能使用前者,否则在循环数组时扩展的函数体也会被当做数据返回. var data = { p1:1, p2:"b& ...