Use ASP.NET Core cross-platform Web document scanning

If you are a C # developer, you can use what technology to create a Web document scanning applications cross-platform? The answer is ASP.NET Core and Dynamic Web TWAIN. In this article, I will share how to use these technologies from scratch to build a simple Web application.

True cross-platform document scanning solutions

Why Dynamic Web TWAIN scanning documents for development? This powerful feature SDK supports Windows, Linux and macOS.

DWT support platform

installation

ASP.NET Core
Dynamic Web TWAIN

Construction of Hello World

How to create a new ASP.NET core project? You can read the tutorial provided by Microsoft .

mkdir aspnetcoreapp
cd aspnetcoreapp
dotnet new -t web

This is a file that contains so many beautiful Web project. Let's create an empty project using the command line:

dotnet new

This command only generates two files Program.cs and project.json. To run the application as a Web server:

  1. Modify Program.cs:
       using System.IO;
       using aspnetcoreapp;
       using Microsoft.AspNetCore.Hosting;
       
       namespace ConsoleApplication
       {
        public class Program
        {
            public static void Main(string[] args)
            {
                var host = new WebHostBuilder()
                    .UseKestrel()
                    .UseContentRoot(Directory.GetCurrentDirectory())
                    .UseStartup<Startup>()
                    .Build();
     
                host.Run();
            }
        }
       }
  1. Project.json to add a dependency:
{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.1"
        },
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
        "Microsoft.AspNetCore.StaticFiles": "1.0.0"
      },
      "imports": "dnxcore50"
    }
  }
}

By dependency, we can run the Web server and load static resources such as HTML files, CSS files and images.

Creating Startup.cs:

using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
 
namespace aspnetcoreapp
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            // app.UseDefaultFiles();
            // app.UseStaticFiles();
            app.UseFileServer();
        }
    }
}

The code will be loaded as the default index.html page.

Create a default static file folder wwwroot . The sample code to copy this folder ** from ** \ Samples. If you want to scan a document uploaded to a Web server, how to do? And save the file path mapping operation, as follows:

using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
 
namespace aspnetcoreapp
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            // app.UseDefaultFiles();
            // app.UseStaticFiles();
            app.UseFileServer();
 
            app.Map("/upload", UploadFile);
        }
 
        private static void UploadFile(IApplicationBuilder app)
        {
            app.Run(async context =>
            {
                var files = context.Request.Form.Files;
                var uploads = Path.Combine(Directory.GetCurrentDirectory(), "uploads");
                if (!Directory.Exists(uploads)) {
                    Directory.CreateDirectory(uploads);
                }
 
                foreach (var file in files)
                {
                    var filename = file.FileName;
                    using (var fileStream = new FileStream(Path.Combine(uploads, filename), FileMode.Create))
                    {
                        await file.CopyToAsync(fileStream);
                    }
                }
            });
        }
    }
}

Now run the Web project:

dotnet restore
dotnet run

Access localhost: 5000 to scan in a Web browser and upload documents.

operation result

Use MVC

If you want to take more action, we recommend using MVC. We look at how to change the code.

In project.json add dependencies:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.1"
        },
        "Microsoft.AspNetCore.Mvc": "1.0.1",
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
        "Microsoft.AspNetCore.StaticFiles": "1.0.0"
      },
      "imports": "dnxcore50"
    }
  },
 
  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  }
}

Adding MVC service and replaced with a route map Startup.cs in MVC:

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
 
namespace aspnetcoreapp
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            app.UseStaticFiles();
 
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
 
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }
    }
}

The default controller for the Home , the default action for the Index . Let wwwroot / index.html move to the Views / Home / the Index.cshtml . I do not want to add any code, but change the file suffix. Create a presentation index.cshtml of the Controllers / HomeController.cs :

using Microsoft.AspNetCore.Mvc;
 
namespace aspnetcoreapp.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

So far, we can run without problems and display the web page. The next step is to realize the function to upload files.

Open index.cshtml and change the operation page:

var strActionPage = CurrentPath + "upload";

To deal with the operation page, we must create the Controllers / UploadController.cs :

using Microsoft.AspNetCore.Mvc;
using System.IO;
 
namespace aspnetcoreapp.Controllers
{
    public class UploadController : Controller
    {
        [HttpPost]
        public void Index()
        {
            var files = Request.Form.Files;
            var uploads = Path.Combine(Directory.GetCurrentDirectory(), "uploads");
            if (!Directory.Exists(uploads))
            {
                Directory.CreateDirectory(uploads);
            }
 
            foreach (var file in files)
            {
                var filename = file.FileName;
                using (var fileStream = new FileStream(Path.Combine(uploads, filename), FileMode.Create))
                {
                    file.CopyTo(fileStream);
                    fileStream.Flush();
                }
            }
        }
    }
}

that's it. Very simple!

Source Code

https://github.com/dynamsoft-dwt/ASP.NET-Core

Released six original articles · won praise 0 · Views 1383

Guess you like

Origin blog.csdn.net/weixin_44795817/article/details/89031747