c# 文件上传下载

本文介绍了如何使用C#的WebClient类在B/S架构下实现文件的上传和下载。通过WebForm作为服务器端,ConsoleApplication作为客户端进行演示。详细讲解了客户端和服务器端的代码实现,并提供了上传和下载的测试步骤。文章强调了环境配置和架构的重要性,适合初学者参考。
摘要由CSDN通过智能技术生成

1. 介绍 

C#文件上传下载用WebClient类实现

微软官方使用Demo

https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadfile?view=netframework-4.7.2

架构为B/S

 

模板

服务器: WebForm

客户端:ConsoleApplication

 

2. 代码

2.1  客户端

解决方案:

WebClientOper.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace WebClientOperation
{
    class WebClientOper
    {
        public bool UploadFile(string address, string fileName)
        {
            if (!File.Exists(fileName))
                return false;

            using (WebClient client = new WebClient())
            {
                client.UploadProgressChanged += Client_UploadProgressChanged; ;
                client.UploadFileCompleted += Client_UploadFileCompleted; ;
                client.UploadFileAsync(new Uri(address), fileName);

                Console.ReadKey();
            }

            return true;
   
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值