How to Upload Pdf Files to Common Application

Background

Many times, nosotros need to work with the file and storing the physical files on the Server, which is very difficult considering it will swallow lots of retentiveness of the Server. Thus, in this commodity, we will larn, how to upload the files in the binary format into the database and download from the database with the help of ASP.Net MVC, using FileResult. Thus, let's learn pace past stride and so the beginners can also sympathise.

Step i - Create MVC Application.

Now, let usa start with a step by footstep approach from the creation of a uncomplicated MVC Awarding in the post-obit-

  1. "Get-go", followed by "All Programs" and select "Microsoft Visual Studio 2015".
  2. Click "File", followed past "New" and click "Project". Select "ASP.Internet Web Application Template", provide the Project a name as you lot wish and click OK. Later on clicking, the following Window volition appear,

    empty

Step 2 - Create Model Class

Now, let united states of america create the model form file, named EmpModel.cs, past right clicking on Models folder and define the following properties in EmpModel.cs class and FileDetailsModel as:

The code snippet of EmpModel.cs and FileDetailsModel .cs will expect like-

  1. public class  EmpModel
  2. {
  3.     [Required]
  4.     [DataType(DataType.Upload)]
  5.     [Display(Proper name ="Select File" )]
  6. public  HttpPostedFileBase files { get ; set up ; }
  7. }
  8. public class  FileDetailsModel
  9. {
  10. public int  Id { get ; set up ; }
  11.     [Display(Name ="Uploaded File" )]
  12. public  String FileName { go ; set ; }
  13. public byte [] FileContent { get ; prepare ; }
  14. }

Step 3 - Create Table and Stored Procedure

Now, create the stored process and the table to store the uploaded files in binary format and display back to the user's view. Use the script, given below, to create the table named FileDetails every bit-

  1. CREATE Table  [dbo].[FileDetails](
  2.     [Id] [int ] IDENTITY(1,1) NOT NULL ,
  3.     [FileName] [varchar ](60) NULL ,
  4.     [FileContent] [varbinary](max ) NULL
  5. )ON  [ Main ] TEXTIMAGE_ON [ PRIMARY ]

As mentioned in the preceding table script, we take created 3 columns Id, which is to identify the files unique key. FileName to store uploaded file proper noun and FileContent to store the uploaded file contents in the binary format.

Create the stored process to insert the file details, using the script, given beneath-

  1. Create Process  [dbo].[AddFileDetails]
  2. (
  3. @FileNamevarchar (60),
  4. @FileContent varBinary(Max )
  5. )
  6. as
  7. brainstorm
  8. Set  NoCount on
  9. Insert into  FileDetails values (@FileName,@FileContent)
  10. Finish

To get the uploaded file details, apply the code, given beneath-

  1. CREATE Procedure  [dbo].[GetFileDetails]
  2. (
  3. @Idint = null
  4. )
  5. as
  6. begin
  7. select  Id,FileName,FileContent from  FileDetails
  8. where  Id= isnull (@Id,Id)
  9. End

We have created the tables and stored procedures. I hope, yous have created the same.

Step 4 - Add Controller Form

Now, let u.s.a. add ASP.NET MVC controller, as shown in the screenshot, given below-

empty

Later on clicking Add button, information technology will prove in the Window. Specify the Controller proper noun as Home with suffix Controller. Now, let's alter the default code of Home controller . Later on modifying the lawmaking of Homecontroller grade, the code will look like-

HomeController.cs

  1. using  FileUploadDownLoadInMVC.Models;
  2. using  System;
  3. using  System.Collections.Generic;
  4. using  Arrangement.IO;
  5. using  System.Linq;
  6. using  System.Web;
  7. using  System.Web.Mvc;
  8. using  Dapper;
  9. using  System.Configuration;
  10. using  System.Information.SqlClient;
  11. using  System.Data;
  12. namespace  FileUploadDownLoadInMVC.Controllers
  13. {
  14. public class  HomeController : Controller
  15.     {
  16.         #region Upload Download file
  17. public  ActionResult FileUpload()
  18.         {
  19. render  View();
  20.         }
  21.         [HttpPost]
  22. public  ActionResult FileUpload(HttpPostedFileBase files)
  23.         {
  24.          String FileExt=Path.GetExtension(files.FileName).ToUpper();
  25. if  (FileExt == ".PDF" )
  26.             {
  27.                 Stream str = files.InputStream;
  28.                 BinaryReader Br =new  BinaryReader(str);
  29.                 Byte[] FileDet = Br.ReadBytes((Int32)str.Length);
  30.                 FileDetailsModel Fd =new  Models.FileDetailsModel();
  31.                 Fd.FileName = files.FileName;
  32.                 Fd.FileContent = FileDet;
  33.                 SaveFileDetails(Fd);
  34. return  RedirectToAction( "FileUpload" );
  35.             }
  36. else
  37.             {
  38.                 ViewBag.FileStatus ="Invalid file format." ;
  39. render  View();
  40.             }
  41.         }
  42.         [HttpGet]
  43. public  FileResult DownLoadFile( int  id)
  44.         {
  45.             List<FileDetailsModel> ObjFiles = GetFileList();
  46.             var FileById = (from FCin  ObjFiles
  47.                             where FC.Id.Equals(id)
  48.                             selectnew  { FC.FileName, FC.FileContent }).ToList().FirstOrDefault();
  49. return  File(FileById.FileContent, "application/pdf" , FileById.FileName);
  50.         }
  51.         

0 Response to "How to Upload Pdf Files to Common Application"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel