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-
- "Get-go", followed by "All Programs" and select "Microsoft Visual Studio 2015".
- 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,
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-
- public class EmpModel
- {
- [Required]
- [DataType(DataType.Upload)]
- [Display(Proper name ="Select File" )]
- public HttpPostedFileBase files { get ; set up ; }
- }
- public class FileDetailsModel
- {
- public int Id { get ; set up ; }
- [Display(Name ="Uploaded File" )]
- public String FileName { go ; set ; }
- public byte [] FileContent { get ; prepare ; }
- }
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-
- CREATE Table [dbo].[FileDetails](
- [Id] [int ] IDENTITY(1,1) NOT NULL ,
- [FileName] [varchar ](60) NULL ,
- [FileContent] [varbinary](max ) NULL
- )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-
- Create Process [dbo].[AddFileDetails]
- (
- @FileNamevarchar (60),
- @FileContent varBinary(Max )
- )
- as
- brainstorm
- Set NoCount on
- Insert into FileDetails values (@FileName,@FileContent)
- Finish
To get the uploaded file details, apply the code, given beneath-
- CREATE Procedure [dbo].[GetFileDetails]
- (
- @Idint = null
- )
- as
- begin
- select Id,FileName,FileContent from FileDetails
- where Id= isnull (@Id,Id)
- 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-
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
- using FileUploadDownLoadInMVC.Models;
- using System;
- using System.Collections.Generic;
- using Arrangement.IO;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Dapper;
- using System.Configuration;
- using System.Information.SqlClient;
- using System.Data;
- namespace FileUploadDownLoadInMVC.Controllers
- {
- public class HomeController : Controller
- {
- #region Upload Download file
- public ActionResult FileUpload()
- {
- render View();
- }
- [HttpPost]
- public ActionResult FileUpload(HttpPostedFileBase files)
- {
- String FileExt=Path.GetExtension(files.FileName).ToUpper();
- if (FileExt == ".PDF" )
- {
- Stream str = files.InputStream;
- BinaryReader Br =new BinaryReader(str);
- Byte[] FileDet = Br.ReadBytes((Int32)str.Length);
- FileDetailsModel Fd =new Models.FileDetailsModel();
- Fd.FileName = files.FileName;
- Fd.FileContent = FileDet;
- SaveFileDetails(Fd);
- return RedirectToAction( "FileUpload" );
- }
- else
- {
- ViewBag.FileStatus ="Invalid file format." ;
- render View();
- }
- }
- [HttpGet]
- public FileResult DownLoadFile( int id)
- {
- List<FileDetailsModel> ObjFiles = GetFileList();
- var FileById = (from FCin ObjFiles
- where FC.Id.Equals(id)
- selectnew { FC.FileName, FC.FileContent }).ToList().FirstOrDefault();
- return File(FileById.FileContent, "application/pdf" , FileById.FileName);
- }
0 Response to "How to Upload Pdf Files to Common Application"
Post a Comment