.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 ...
随机推荐
- tomcat的一些优化及报错
以下为转发来,具体地址为 http://blog.csdn.net/chen3888015/article/details/7432488 环境centos5.7 tomcat6 http://apr ...
- Linux—挂载磁盘(云盘)
创建挂载目录 [root@localhost ~]# mkdir -p /www 可以看到/dev/vda1盘挂载/ /dev都是位于根路径下,都属于系统盘.根路径 / 都是位于系统盘.而/root, ...
- JUC-9-线程按序交替
package com.wf.zhang.juc; import java.util.concurrent.locks.Condition; import java.util.concurrent.l ...
- go语言设计模式之Command
package main import ( "fmt" ) type Command interface { Execute() } type ConsoleOutput stru ...
- 201871010126 王亚涛 《面向对象程序设计(java)》 第一周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/wyt0455820/ ...
- python包matplotlib绘制图像
使用matplotlib绘制图像 import matplotlib.pyplot as plt from matplotlib.pyplot import MultipleLocator impor ...
- acwing 7 混合背包
习题地址 https://www.acwing.com/problem/content/description/7/ 题目描述有 N 种物品和一个容量是 V 的背包. 物品一共有三类: 第一类物品只 ...
- LeetCode 200. 岛屿数量
习题地址 https://leetcode-cn.com/problems/number-of-islands/ 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水 ...
- Java多态的总结
多态 多态是一个对象具有不同表现形态或形式的能力,根据不同的实例执行不同的操作,例如打印机具有打印功能,打印机又有彩色打印机和黑白打印机,彩色打印机的实例打印出来的是彩色,黑白打印机打印出来的是黑色, ...
- CF750G New Year and Binary Tree Paths(DP)
神仙题.为啥我第一眼看上去以为是个普及题 路径有两种,第一种是从 LCA 一边下去的,第二种是从 LCA 两边都下去了的. 先考虑第一种. 先枚举路径长度 \(h\). 当 LCA 编号是 \(x\) ...