Wednesday, January 29, 2014

accept only pdf files in file upload

 <input type="file" name="fup" id="fup" required="required" accept="application/pdf"/>

How to upload larger files in MVC4?


  <system.webServer>
<security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483648"/>
      </requestFiltering>
    </security>
 </system.webServer>

How to View PDF Files in MVC 4?

@Html.ActionLink(item.DFile,"DownloadFile",new { file = item.DFile},new { @style = "color:#000;text-decoration:underline",target = "_blank"})


 public ActionResult DownloadFile(string file)
        {
            var fileStream = new FileStream(Server.MapPath("~/Uploads/") + file,
                                             FileMode.Open,
                                             FileAccess.Read
                                           );
            var fsResult = new FileStreamResult(fileStream, "application/pdf");
            return fsResult;
        }

how to upload and download file in MVC4?


 <input type="file" name="fup" id="fup"/>
Upload File:
----------------
Random rdm = new Random();
            HttpPostedFileBase fup = Request.Files["fup"];
            string uploadedFileName = string.Empty;
            string path = string.Empty;
            if (fup.ContentLength > 0)
            {
                string no = Convert.ToString(DateTime.Now.Millisecond * rdm.Next(10000));
                string extension = System.IO.Path.GetExtension(fup.FileName).ToString();
                string fname = System.IO.Path.GetFileNameWithoutExtension(fup.FileName)+"_"+no;
                uploadedFileName = fname.Replace(" ", "_") + extension;
                path = Path.Combine(Server.MapPath("~/Uploads/"), uploadedFileName);
                fup.SaveAs(path);
            }


Download File:
-------------------

            var contentDisposition = string.Format("attachment; filename={0}", file);
            HttpContext.Response.AddHeader("Content-Disposition", contentDisposition);
            HttpContext.Response.ContentType = "application/force-download";
            HttpContext.Response.BinaryWrite(System.IO.File.ReadAllBytes(Path.Combine(Server.MapPath("~/Uploads/"), file)));

Wednesday, January 22, 2014

How to Check all the Checkboxes and delete using Jquery?

    <input type="checkbox" id="chkall" onclick="return checkAll(this.checked)" />
  <input type="button" name="button" id="btndel" value="Delete" class="redbutton" onclick="return DeleteFunction()" >

<script src="@Url.Content("~/Content/js/jquery.tablesorter-2.0.3.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Content/js/jquery.tablesorter.filer.js")" type="text/javascript"></script>
<script type="text/javascript" src="@Url.Content("~/Content/js/jquery.tablesorter.pager.js")"></script>

<script type="text/javascript">
function checkAll(checkedStatus) {
        $("input[class='chkuserid']").each(function () {
            this.checked = checkedStatus;
        });
    }

  function DeleteFunction() {
        var funarray = [];
        $('.chkuserid:checkbox:checked').each(function () {
            funarray.push($(this).val());
        });
     
        if (funarray.length == 0) {
            alert('Please Select Atleast One Record to Delete.');
        } else {
            var t = confirm('Are you sure you want to delete?');
            if (t == true) {
                window.location = '@Url.Action("DeleteFunction","Admin")' + '?FUNC_ID=' + funarray;
            }
            else{return false;}
        }
     
    }
</script>