咱們在作項目的時候常常會遇到傳附件的問題,一是若是附件不符合格式要求,咱們要清空input file。二是點擊附件超連接的時候若是附件不在,應該提示附件不存在。下來咱們就看看怎麼實現。先看一個控制器javascript
- /// <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", "p_w_upload; 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;
- }
再看看jsjava
- 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值便可。ajax
最後來個實惠的,把sql轉化爲linqsql
- 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命令。數據庫