ProjectManager.aspx
前端
<%:Styles.Render("~/Content/bootstrap-table")%>
<%:Styles.Render("~/forms/artDialog/skins/default2.css")%>
<%:Styles.Render("../CSS/fileinput.min.css")%>
<%:Scripts.Render("~/bundles/bootstrap-table")%>
<%:Scripts.Render("~/bundles/bootstrap-dialog")%> <%:Scripts.Render("../JS/fileinput.min.js")%> <%:Scripts.Render("~/forms/artDialog/artDialog.js")%>
rows.push({
field: 'Image',
title: '项目图片',
halign: 'center',
align: 'center',
width: '80px',
formatter: function (value, row, index) {
if (!!value) {
return "<a href='" + GetAbsoluteUrl() + value + "' target='_blank' ><img style='width:20px;height:20px;' src='" + GetAbsoluteUrl() + value + "' /></a>";
} else {
return " <a data-id='" + row.Id + "' href=\"javascript:;\" οnclick=\"showUpalod('" + row.Id + "','Image')\">上传</a>";
}
}
});
<!-- 模态框1(Modal) -->
<div class="modal fade" id="upImg" data-target="" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
×
</button>
<h4 class="modal-title" id="H1">上传文件
</h4>
</div>
<div class="box-body">
<div class="modal-body" id="liuchengbody">
<input type="hidden" id="key" />
<input id="faifile" name="faifile" type="file" data-show-preview="false">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
关闭
</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal -->
</div>
function showUpalod(id, type) {
$('#key').val(id);
initFileInput("faifile", "Project.ashx?action=FileUpload&type=" + type + "&rnd=" + Math.random());
$('#upImg').modal('show');
}
function initFileInput(ctrlName, uploadUrl) {
var control = $('#' + ctrlName);
control.fileinput('destroy');
control.fileinput({
uploadAsync: true, //异步上传
language: 'zh', //设置语言
uploadUrl: uploadUrl, //上传的地址
allowedFileExtensions: ['jpg', 'png', 'pdf'],//接收的文件后缀
maxFileCount: 1, //表示允许同时上传的最大文件个数
maxFileSize: 6144,//单位为kb,如果为0表示不限制文件大小
showCaption: true,
showUpload: true,
showRemove: true,
showClose: true,
previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
uploadExtraData: function (previewId, index) { //额外参数的关键点
var data = {
id: $('#key').val()
};
return data;
},
}).on('fileuploaded', function (event, data, previewId, index) {
var res = data.response; //返回结果
if (res.result == "1") {
alert("上传成功!");
loadTb();
} else {
alert("上传失败!");
}
});
}
后台
public void FileUpload(HttpContext context)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
try
{
HttpPostedFile file = context.Request.Files["faifile"];
string id = context.Request["id"];
string type = context.Request["type"];
string savepath = "~/"; //父目录
string fileName = GetOrderNumber();
string day = DateTime.Now.ToString("yyyy-MM-dd");
string Pfpath = context.Server.MapPath(savepath) + "UploadFile//StudyTour//" + day;
if (!Directory.Exists(Pfpath))//查看存储路径的文件是否存在
{
Directory.CreateDirectory(Pfpath); //创建文件夹,并上传文件
}
string imgUrl = fileName + "." + file.FileName.Substring(file.FileName.LastIndexOf('.') + 1);
savepath = Pfpath + "//" + imgUrl;
file.SaveAs(savepath);
file.InputStream.Close();
file.InputStream.Dispose();
if (!string.IsNullOrEmpty(id))
{
AddProjectImg(Convert.ToInt64(id), "/UploadFile/StudyTour/" + day + "/" + imgUrl, type);
Attachment attfile = new Attachment();
attfile.ReferID = int.Parse(id);
attfile.FormArea = type;
attfile.FileName = file.FileName;
attfile.ContentType = file.ContentType;
attfile.FileSize = file.ContentLength;
attfile.FileExt = file.FileName.Substring(file.FileName.LastIndexOf('.'));
attfile.UploadUser = "admin";
attfile.FilePath = "/UploadFile/StudyTour/" + day + "/" + imgUrl;
attfile.UploadTime = DateTime.Now;
attfile.IsEnable = true;
attfile.ProcessType = (type == "pdf" ? "项目简介" : "项目封面");
attfile.Insert();
}
dic.Add("result", "1");
}
catch (Exception ex)
{
//失败时返回的参数必须加 error键
dic.Add("result", 0);
}
string json = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
context.Response.ContentType = "json/plain";
context.Response.Clear();
context.Response.Write(json);
context.Response.End();
}
/// <summary>
/// 根据日期生成名称
/// </summary>
/// <returns></returns>
public static string GetOrderNumber()
{
string num = DateTime.Now.ToString("yyMMddHHmmssfff");
return num + Number(3, true).ToString();
}