Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ASP.NET to make a cross-platform document scanning application

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use ASP.NET to do a cross-platform document scanning application". In daily operation, I believe that many people have doubts about how to use ASP.NET to do a cross-platform document scanning application. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for everyone to answer the question of "how to use ASP.NET to do a cross-platform document scanning application". Next, please follow the editor to study!

ASP.NET Core has got a good response among developers because of its excellent performance, good cross-platform features and perfect documentation support. From now on, C # Web application developers can use the existing technology stack to develop cross-platform Web applications.

Preparatory work

First, we need to install Dynamic Web TWAIN to carry out our development work. If you haven't installed this SDK before, you can go to the Dynamic Web TWAIN download page to download the latest version.

Second, for developers who have never used C #, you may also need to install the .net Core environment. Go to Microsoft's download page and download .net Core SDK (Note: developers need to download and install SDK,Runtime included in SDK without additional download).

We strongly recommend that developers use Visual Studio for development on Windows, which will greatly reduce the configuration effort.

Create a project

Regardless of any project development, creating an engineering project is always the first step. Here I will focus on how to create a project using Visual Studio.

Create a project with Visual Studio

For Windows users, creating an ASP.NET Core MVC project is as easy as drinking milk tea. Visual Studio provides templates and automation tools to help developers create projects automatically, and in many cases developers can put the project into use with only minor modifications.

First, on the lower right side of the welcome page, select Create a new project.

Then, on the page where you created the project, select ASP.NET Core Web Application.

Next, Visual Studio asked us to provide a name. We call it dwtDotCore.

Finally, we need to choose the type of ASP.NET Core Web Application, which is recommended here as Model-View-Controller. We will implement Dynamic Web TWAIN-related content in View later. In addition, we will create an API Controller later to implement the file upload requirements.

At this point, the project is created.

Create a project with a command line tool

For Linux users, project creation can only be done through CLI. Start the terminal, change the working directory to the path where the project needs to be stored, and then enter the following command

Dotnet new webapp-o dwtDotNet-- no-https

This command will create an ASP.NET Core MVC project, but not as comprehensive as Visual Studio.

Configure the project

Let's import Dynamic Web TWAIN now. Usually you can find the Resources folder in C:\ Program Files (x86)\ Dynamic\ Dynamic Web TWAIN SDK, which contains the required working files.

We copied the Resources folder to / wwwroot/lib and renamed Resources to dwt.

Go to the dwt folder, open dynamsoft.webtwain.config.js, find the Dynamsoft.WebTwainEnv.ResourcesPath field, uncomment it, and change the value to'/ lib/dwt'

The loading of Dynamic Web TWAIN depends on several resource files, so you need to specify clearly the relative path where the resource files are located after release, otherwise a 404 NOT FOUND error will be reported.

Find the Dynamsoft.WebTwainEnv.Containers field, you can modify or remember ContainerId, in View we need to create an element with this ID.

Function development

Before developing, let's take a look at the structure of the project.

Readers who create projects through CLI can refer to this diagram to complete files that the CLI tool did not create, or to use the dotnet-aspnet-codegenerator tool to generate missing files. Of course, we will attach all the source code at the end, and you can also do further development based on the source code.

Create View

Let's create a scanning interface. Before that, remember to remove the existing code.

Paste the following code into index.cshtml

@ {ViewData ["Title"] = "Home Page" } Upload JPG PDF TIFF File Name: function AcquireImage () {const DWObj = Dynamsoft.WebTwainEnv.GetWebTwain ('dwt-container') if (DWObj) {if (DWObj.UseLocalService) {DWObj.SelectSource (function () {var OnAcquireImageSuccess = OnAcquireImageFailure = function) () {DWObj.CloseSource ()} DWObj.OpenSource () DWObj.IfDisableSourceAfterAcquire = true DWObj.AcquireImage (OnAcquireImageSuccess OnAcquireImageFailure)}, function () {console.log ('SelectSource failed')})} else {DWObj.LoadImageEx ('') -1)}} function Upload () {const host = location.hostname const protocol = location.protocol const uploadPath ='/ api/File' let uploadFileName = document.getElementById ('filename-input'). Value const port = location.port | (protocol =' https:'? 443: 80) var formatSelector = document.getElementsByName ('format') let Format = (selector = > {let select =''selector.forEach (e = > {if (e.checked) {select = e.value}}) uploadFileName = uploadFileName +'.'+ select switch (select) {case 'jpg': {return Dynamsoft.EnumDWT_ImageType.IT_JPG} case 'pdf': {return Dynamsoft.EnumDWT_ImageType.IT_PDF} case' tif': {return Dynamsoft.EnumDWT_ImageType.IT_TIF}) (formatSelector) let uploadFormat = Dynamsoft.EnumDWT_UploadDataFormat.Binary const DWObj = Dynamsoft.WebTwainEnv.GetWebTwain ('dwt-container') if (DWObj) { DWObj.HTTPPort = port DWObj.IfSSL = true let indices = DWObj.SelectedImagesIndices DWObj.HTTPUpload (protocol +'/ /'+ host +':'+ port + uploadPath Indices, format, uploadFormat, uploadFileName, () = > {alert ('success')}, (errCode, errStr, res) = > {console.error (`${errCode}: ${errStr}. Server return: ${res} `)})}}

In particular, it is important to note that dynamsoft.webtwain.initiate.js and dynamsoft.webtwain.config.js need to be introduced separately and sequentially.

We set up two buttons, a set of radio options, and an input box to provide scanning and upload functions.

Realize file upload

We provide file upload service through additional API.

Right-click the Controller folder and select Add-> New Scaffolded Item...

In the pop-up window, select API Controller with read/write actions.

Then, only the file name is changed. In a subsequent window, name the file name FileController.cs.

Finally, change the newly created Controller to the code shown below

Using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Mvc / / For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860namespace dwtDotCore.Controllers {[Route ("api/ [controller]")] [ApiController] public class FileController: ControllerBase {/ / POST api/ [HttpPost] public async Task Upload () {var files = Request.Form.Files; var path = Path.Combine (Directory.GetCurrentDirectory (), "Upload") If (! Directory.Exists (path)) {try {Directory.CreateDirectory (path);} catch (Exception e) {Debug.WriteLine (e.StackTrace.ToString ()) Return Unauthorized ("not able to create");}} foreach (var uploadFile in files) {var fileName = uploadFile.FileName; using (var stream = System.IO.File.Create (Path.Combine (path, fileName) {await uploadFile.CopyToAsync (stream) }} return Ok ();} run the program

Now that we have finished writing all the code, let's test the following program functions.

Visual Studio users, who click the run button at the top to run the program.

CLI users run the program by entering the following command on the command line

Dotnet restoredotnet run

Once the server starts normally, you will see a rectangle at the bottom of the page in the browser window that pops up, indicating that the Dynamic Web TWAIN has been loaded successfully.

Click Scan to jump out of the scan source selector.

Scan successful.

Click upload to upload the scanned document to the server.

Confirm the upload of the file under the folder specified by the server.

At this point, the study on "how to use ASP.NET to do a cross-platform document scanning application" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report