我们在做项目的时候经常会遇到传附件的问题,一是如果附件不符合格式要求,我们要清空input file。二是点击附件超链接的时候如果附件不在,应该提示附件不存在。下来我们就看看怎么实现。先看一个控制器
- /// <summary>
- /// 检查文件是否存在
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public JsonResult CheckFileExist(string id)
- {
- Appurtenances appurtenances = messageEntities.appurtenances.FirstOrDefault(app => app.serial == id);
- string fileName = appurtenances.file_name;
- string filePath = Server.MapPath(appurtenances.file_path);
- if (!System.IO.File.Exists(filePath))
- {
- strJson = "{suc:0,msg:'文件不存在'}";
- }
- else
- {
- strJson = "{suc:1,msg:'文件存在'}";
- }
- return Json(strJson, JsonRequestBehavior.AllowGet);
- }
- /// <summary>
- /// 附件下载
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public ActionResult GetFile(string id)
- {
- Appurtenances appurtenances = messageEntities.appurtenances.FirstOrDefault(app => app.serial == id);
- string fileName = appurtenances.file_name;
- string filePath = Server.MapPath(appurtenances.file_path);
- FileStream f = new FileStream(filePath, FileMode.Open);
- byte[] bytes = new byte[(int)f.Length];
- f.Read(bytes, 0, bytes.Length);
- f.Close();
- Response.ContentType = "application/octet-stream";
- Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName + appurtenances.file_path.Substring(appurtenances.file_path.LastIndexOf(".")), System.Text.Encoding.UTF8));
- Response.BinaryWrite(bytes);
- Response.Flush();
- Response.End();
- return null;
- }
再看看js
- function checkFileExist(myId) {
- $.ajax({
- url: "/Message/CheckFileExist/" + myId,
- datatype: "json", // 返回的数据类型
- type: "post",
- //async:false,
- success: function (data) {
- var json = eval("(" + data + ")");
- new BaseUtil(json).parsJSONError;
- if (json.suc == 0) {
- jAlert(json.msg, "提示信息");
- } else {
- $("#downLoadSendMsg" + myId).attr("href", "/Message/GetFile/" + myId);
- $("#downLoadSendMsg" + myId).removeAttr("onclick");
- if (window.ActiveXObject) {//IE
- document.getElementById("downLoadSendMsg" + myId).click();
- }
- }
- }
- });
- }
通过上面的代码我们可以看到,如果附件存在,则可以被打开或者下载,如果不存在,会提示文件不存在。 那么对于附件不符合条件,我们如何清空它呢。看看js
- function readedMesAddUpdateAppendix(readedMesNum, rowid, readedMesUrl, readAppdName) {
- var readedMesNum = readedMesNum + 1;
- var mesupdNewRow = "<tr><td width='430px' height='15px' align='left'>附件" + readedMesNum + ": ";
- mesupdNewRow += "<a href='javascript:void(0);' id='downLoadSendMsg" + rowid + "' onclick=checkFileExist('" + rowid + "')>" + readAppdName + "<\/a><\/td><\/tr>";
- jQuery("#readedMesShowAppdTabl").append(mesupdNewRow);
- }
- function deleteFile(file) {
- var ie = (navigator.appVersion.indexOf("MSIE") != -1); //IE
- var ff = (navigator.userAgent.indexOf("Firefox") != -1); //Firefox
- if (ie)
- refreshUploader($("input[name=" + file + "]")[0]);
- else
- $("input[name=" + file + "]").attr("value", "");
- }
- function refreshUploader(file) {
- var file2 = file.cloneNode(false);
- file2.onchange = file.onchange;
- file.parentNode.replaceChild(file2, file);
- }
调用的时候仅需要传一个input file的name值即可。
最后来个实惠的,把sql转化为linq
- select sourseware_id,COUNT(*) as num from cours_ewares as a group by a.sourseware_id having COUNT(*) >1
- List<IGrouping<string,EPA_Cours_Ewares>> courseEwaresList = (from ce in examinationPlanArrangementsEntities.cours_ewares
- group ce by ce.sourseware_id into c where c.Count()>1
- select c).ToList();
关于分页,在写分页的时候先得到IQueryable<T>类型,然后再获得其count(),算出总页数和总记录数后,将IQueryable<T>转化为List<T>,因为IQueryable不会向数据库发送sql命令。