首先需要引入外部js插件 在项目中,使用的jquery版本为 1.7.2
<!--弹出div为窗体插件 PS:该插件可以不引入-->
<script src="../../Scripts/jquery.tools.min.js" type="text/javascript"></script>
<!--异步提交插件-->
<script src="../../Scripts/jquery.form.js" type="text/javascript"></script>
一、 js代码如下
function stuContinueCommunication(feedbackQuestionId) {
// 获取需要提交的信息
var textContent = $("#txt_content_" + feedbackQuestionId).val();
var imgContent = "";
// var urls = "../../Handler/HCommon/HFeedbackCommunication.ashx";
// var params = { type: "stuContinueAsk", FeedbackQuestionId: feedbackQuestionId, ImgContent: imgContent, TextContent: textContent, t: new Date().getMilliseconds() };
$("#frmQuery").ajaxSubmit(
{
url: "../../Handler/HCommon/HFeedbackCommunication.ashx?type=stuContinueAsk&FeedbackQuestionId=" + feedbackQuestionId + "&TextContent=" + textContent + "&t=" + new Date().getMilliseconds(),
type: "post",
dataType: "json",// 可以不指明返回数据格式。默认为返回字符串
success: function (resultData) {
debugger
console.log(resultData);
if (resultData.ResultStatus == "1") {
alert("追问成功");
queryContent();
return;
}
if (resultData.ResultStatus == "0") {
alert("亲,该问题已经反馈过了。");
return;
}
else {
alert("问题回复失败");
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
debugger
alert("问题回复失败,错误信息:" + XmlHttpRequest);
}
}
);
}
二、HTML代码段如下:
注意:
1、添加的 form标签需要 id method enctype 三个属性(有待验证method 属性是否是必须的)
2、input file标签 必须包含 name 属性。否则后台获取不到
<form id="frmQuery" method="post" enctype="multipart/form-data">
<!--public/images/jn_15.jpg-->
<div class="huifu" id="div_huifu_1" style="display: none;">
<div class="pic_hf">
<img src="../../image/pagesimgs/jn_15.jpg" alt="" /></div>
<div class="hf_con">
<textarea name="" id="txt_content_1">请在此输入内容</textarea>
<a href="javascript:void(0);">插入图片</a><div id="img1">
</div>
<input type="file" name="img_1" id="img_1" οnchange="previewImage(this,'img1')" />
<input type="button" class="btn-fk" οnclick="stuContinueCommunication('1')" value="提交反馈" />
</div>
<div class="clear">
</div>
</div>
</form>
三、asp.net 后台代码如下
注意:
1、一般处理程序的类型一定要是context.Response.ContentType = "text/html;charset=UTF-8"; 否则获取不到提交的文件
2、imgIndexName 是由主键+其他信息确定获取指定的文件(此处为上传图片)
public class HFeedback : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// context.Response.ContentType = "text/plain"; //handler默认的类型
context.Response.ContentType = "text/html;charset=UTF-8"; // 需要使用的类型 否则无法获取提交的文件
string requestType = context.Request["type"];
#region 添加回复
if (requestType == "replyFeedback")
{
string feedbackQuestionId = context.Request["feedbackQuestionId"];
Int64 intFeedbackQuestionId = string.IsNullOrEmpty(feedbackQuestionId) ? -1 : Convert.ToInt64(feedbackQuestionId);
string FromUserId = user.UserData.LoginName;
int FromUserRoleType = user.UserData.Identity;
string ToUserId = context.Request["ToUserId"];
string strToUserRoleType = context.Request["ToUserRoleType"];
int ToUserRoleType = string.IsNullOrEmpty(strToUserRoleType) ? -1 : Convert.ToInt16(strToUserRoleType);
string TextContent = context.Request["TextContent"];
string ImgContent = "";
DateTime CreateTime = DateTime.Now;
// 0 已回复过 1 一次都未回复
string isFirstFeedback = context.Request["isFirstFeedback"];
DealInfoResult dealInfoResult = new DealInfoResult();
try
{
List<string> lstImgType = new List<string> { ".jpg", ".png", ".jpeg", ".bmp", ".gif", ".psd", ".svg" };
string imgName = "";
string imgIndexName = "img_" + feedbackQuestionId + "";// 按照名称获取提交的文件,避免取多个文件进行上传
System.Web.HttpFileCollection files = context.Request.Files;
if (files != null && files.Count > 0 && files[imgIndexName] != null)
{
System.Web.HttpPostedFile postedfile = files[imgIndexName];
string FileExtension = "";
if (postedfile.FileName != "")
{
imgName = postedfile.FileName;
FileInfo fi = new FileInfo(imgName);
imgName = fi.Name;
FileExtension = fi.Extension;
if (lstImgType.Contains(FileExtension.ToLower().Trim()))
{
// 上传图片到指定目录
string filepath = context.Server.MapPath("~/Resource/FeedbackImgs/");
//判断文件是否小于10Mb
if (postedfile.ContentLength > 10485759)
{
context.Response.Write("上传图片不能大于10MB!");
return;
}
// 判断文件是否已经导入过
if (!Directory.Exists(filepath))
{
Directory.CreateDirectory(filepath);
}
if (File.Exists(filepath + imgName))
{
ImgContent = imgName;
}
else
{
//上传文件并指定上传目录的路径
postedfile.SaveAs(filepath + imgName);
ImgContent = imgName;
}
}
}
else
{
context.Response.Write("文件无效!");
return;
}
}
dealInfoResult.ResultStatus = feedbackBLL.AddFeedbackCommunicationAdmin(modelFeedbackCommunication) == true ? 1 : 0;
}
catch (Exception ex)
{
dealInfoResult.ResultStatus = -1;
dealInfoResult.ExceptionInfo = ex.Message;
}
context.Response.Write(JsonConvert.SerializeObject(dealInfoResult));// 插件将对象转换为json格式
return;
}
#endregion
}
}