后端代码:

/// <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上传图片的更多相关文章

  1. 百度ueditor上传图片时如何设置默认宽高度

    百度ueditor上传图片时如何设置默认宽高度 一.总结 一句话总结:直接css或者js里面限制一下就好,可以用html全局限制一下图片的最大高度 直接css或者js里面限制一下就好,可以用html全 ...

  2. 百度UEditor上传图片-再总结一次

    晚上,在继续开发BriefCMS,把百度UEditor上传图片的问题,给解决了,终于解决了. 公司极简版CMS.BriefCMS.个人官网,最近2个月,与百度UEditor厮杀了好久.最值得吐槽的,就 ...

  3. 中小研发团队架构实践之生产环境诊断工具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如同医生的听诊器,是系统生病时做问题诊断的逆向分析工具 ...

  4. angularjs + fis +modJS 对于支持amd规范的组建处理(PhotoSwipe 支持,百度webUpload支持)

    这不是很好的处理方式,但是能够解决问题,希望有大神推荐更好的方式. 前端模块使用angularjs + fis +modJS 开发前端应用有两个月了.总结了以下的优点: fis 自动构建,自动发布,功 ...

  5. 使用.Net Core+IView+Vue集成上传图片功能

    最近的项目里有上传图片的功能,当然这个功能在项目里是必须要有的,那么目前这个项目是使用完全的前后端分离,在选择文件上传的组件中还是选择了全面支持Vue的IView,任何上传图片都是通过HTTP请求,服 ...

  6. 使用Webupload上传图片到FastDFS分布式文件系统

    使用Webupload插件上传图片到FastDFS分布式文件系统. 前提条件:1.已安装FastDFS分布式文件系统 2.使用webuploader插件上传文件 3.maven工程已引入FastDFS ...

  7. .net core 2.0 webuploader上传图片

    引入文件 <link href="~/Scripts/webuploader-0.1.5/webuploader.css" rel="stylesheet" ...

  8. 百度ueditor 上传图片后如何设置样式

    最近项目中遇到一个问题,UEditor上传图片后,在内容展示会修改图片样式.但是表情也是img标签,所以全局修改是有问题的, 所以只能着手修改一下插件的代码. 首先找到图片上传的服务器段文件.这里主要 ...

  9. 关于 百度 Ueditor 上传图片时 打开文件夹的延迟问题

    在使用 ueditor 开发时, 作为一个web文本编辑器使用时. 当点击上传图片时, 文件夹要延迟好久才能打开. 解决: 针对多图片上传, 将/ueditor/dialogs/image/image ...

随机推荐

  1. JavaScript---动态加载script和style样式

    一个网页里面的内容理解为一个XML或者说网页本身也就是一个XML文档,XML文档都有很特殊的象征:"标签"也叫"节点". 一个基本的网页格式 <!DOCT ...

  2. https连接

        在发送连接之前设置显示握手过程:   System.setProperty("javax.net.debug", "all"); DubboServer ...

  3. 程序运行时间测试 - 使用libc 中 time 函数 实现秒级的运行时间检测

    c 标准库中,有time 函数,可以返回 1970年1月1日 开始到现在的秒数,我们可以调用两次的时间差来计算程序运行时间: https://github.com/yaowenxu/Workplace ...

  4. 使用vue-cli搭建spa项目

    1. 什么是vue-cli? vue-cli是vue.js的脚手架,用于自动生成vue.js+webpack的项目模板,创建命令如下: vue init webpack xxx 注1:xxx 为自己创 ...

  5. elementui入门以及nodeJS环境搭建

    1. ElementUI简介 我们学习VUE,知道它的核心思想式组件和数据驱动,但是每一个组件都需要自己编写模板,样式,添加事件,数据等是非常麻烦的, 所以饿了吗推出了基于VUE2.0的组件库,它的名 ...

  6. UVA 1473 Dome of Circus

    https://cn.vjudge.net/problem/UVA-1473 题目 给出一些点,问包含这些点的最小圆锥(要求顶点在y轴,底面圆心在原点)的体积 题解 因为圆锥对称,所以可以把所有点旋转 ...

  7. isinstance和issubclass

    目录 一.isinstance与type 二.issubclass 一.isinstance与type 在游戏项目中,我们会在每个接口验证客户端传过来的参数类型,如果验证不通过,返回给客户端" ...

  8. lua require路径设置实例

    1.首先要强调的是,lua require的路径用的是斜杠"/",而不是从Windows文件属性那里复制来的反斜杠"\". 2.通过 print(pagckag ...

  9. IT兄弟连 HTML5教程 了解HTML5的主流应用1

    在很多人眼里,HTML5与互联网营销密切相关,但其实从开发者的角度而言,它是一种网页标准,定义了浏览器语言的编写规范.伴随HTML5标准尘埃落定,浏览器对HTML5特性的逐步支持,再加上国内对HTML ...

  10. Linux中的buff/cache内存

    我们用free.top等相关能够查询到当前内存的使用情况的命令时,总会有一个buff/cache让我们很困惑. buffer 即写如磁盘时,先保存到磁盘缓冲区(buffer),然后再写入到磁盘. ca ...