页面代码:
<div id="fileQueuePlug"></div>
<input type="file" name="uploadifyplug" id="uploadifyplug" />
<a href="javascript:$('#uploadifyplug').uploadifyUpload();">上传</a>
<input id="windowspathplug" type="hidden" value="" />
脚本加载ckeditor插件并提交:
$(function () {
$("#uploadifyplug").uploadify({
'uploader': '/Scripts/uploadify/uploadify.swf',
'script': '/ToolsAndPlug/UploadImage',
'cancelImg': '/Scripts/uploadify/uploadify-cancel.png',
'folder': '/Image/Uploads/ToolsOrPlug',
'queueID': 'fileQueuePlug',
'auto': false,
'multi': false,///设置true可实现多条上传
'fileExt': '*.jpg;*.png;*.jpeg;*.gif;*.bmp',
'fileDesc': '请选择图片类型文件',
'sizeLimit': 1024 * 1024 * 10,
'onSelect': function (e, queueId, fileObj) {
$("#uploadifyplug").uploadifySettings('scriptData', { 'windowspathjs': $("#windowspathplug").val() });
},
'onComplete': fun
});
});
function fun(event, queueId, fileObj, response, data) {
if (response != "") {
$.messager.show({
title: '提示',
msg: '上传成功',
timeout: 5000,
showType: 'slide'
});
var displaypath;
var windowspath;
var arr = new Array();
arr = response.split("|");
displaypath = arr[0];
windowspath = arr[1];
$("#plugortoolsimage").attr("value", displaypath);
$("#windowspathplug").attr("value", windowspath);
}
else {
$.messager.show({
title: '提示',
msg: '上传失败',
timeout: 5000,
showType: 'slide'
});
}
}
C#页面:
public ContentResult UploadImage(HttpPostedFileBase FileData, string folder, string windowspathjs = "")
{
///验证图片是否存在进行覆盖操作
if (windowspathjs != "")
{
if (System.IO.File.Exists(windowspathjs))
{
System.IO.File.Delete(windowspathjs);
}
}
///定义变量拼接图片的相对路径
string response = "";
///定义变量存储物理路径
string windowspath = "";
if (FileData != null)
{
///获取上传文件的后缀名
string fileExtension = Path.GetExtension(FileData.FileName);
///为上传的文件取新的名字含后缀
string buildName = DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension;
///生产文件存放的物理路径
string savepath = Request.MapPath("~" + folder + "/");
///检查物理路径是否存在,不存在则创建
if (!Directory.Exists(savepath))
{
Directory.CreateDirectory(savepath);
}
///拼接完成的文件物理路径含文件名
windowspath = savepath + buildName;
///保存文件
FileData.SaveAs(windowspath);
///拼接相对路径
response = "../.." + folder + "/" + buildName;
}
return Content(response + "|" + windowspath);
}