The WPF client uploads files to the ASP. net mvc website.

Source: Internet
Author: User

This document describes how to upload files from a client to an ASP. net mvc website. The uploaded files can be used in hours. If the file size is large, it may be more suitable for uploading via ftp.

Server

Since the client needs to upload files to the server, the server must be able to collect files. In ASP. net mvc websites, it is easy to upload files from clients. Because you do not need to return a View to the client, you only need to communicate with the client, so you do not need to create a view. Create a new controller and add an action to the existing controller to process file uploads.

 

1 ///   <Summary>
2 /// Upload
3 ///   </Summary>
4 ///   <Param name = "file"> Uploaded file information </Param>
5 ///   <Returns> </returns>
6 [Httppost] // Allow data submission in post Mode
7 Public   Int Upload (httppostedfilebase file)
8 {
9 File. saveas (server. mappath (string. Format ( " /Uploadfiles/{0} " , File. filename ))); // Save files
10 Return   0 ; // Return data to the client
11 }

 

The parameter name file is also the key name of the post data. The client must also include this name when uploading data.

Client

The client uses WPF or winform to obtain the path of the file to be uploaded, and then upload the file to the server.

 

1 ///   <Summary>
2 /// Select File
3 ///   </Summary>
4 ///   <Param name = "sender"> </param>
5 ///   <Param name = "E"> </param>
6 Private   Void Select_click ( Object Sender, routedeventargs E)
7 {
8 Openfiledialog Dialog =   New Openfiledialog (); // Initialization dialog box
9 Dialog. multiselect =   False ; // Multiple files cannot be selected.
10 Dialog. showdialog (); // Show dialog box
11 Filepath. Content = Dialog. filename; // Display the file path on the Interface
12 Upload. isenabled =   True ; // Allow the upload button
13 }
14
15 ///   <Summary>
16 /// Upload File button
17 ///   </Summary>
18 ///   <Param name = "sender"> </param>
19 ///   <Param name = "E"> </param>
20 Private   Void Upload_click ( Object Sender, routedeventargs E)
21 {
22 String URL = " Http: // localhost: 9741/sample/upload " ; // Server URL of the uploaded file
23 WebClient =   New WebClient (); // Initialize WebClient
24 WebClient. uploadfile (URL, filepath. content. tostring ()); // Upload files
25 }

 

When a file is uploaded by WebClient, the default key is file, so no explicit settings are provided here. If you use this method to upload data, you may need to provide this name so that the server can receive files.

In this example, the server does not verify the client or uploaded files. Security considerations should be taken into account during formal use.

 


Sample download

 

 

This article applies to. NET framework 4

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.