亚洲乱色熟女一区二区三区丝袜,天堂√中文最新版在线,亚洲精品乱码久久久久久蜜桃图片,香蕉久久久久久av成人,欧美丰满熟妇bbb久久久

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#實(shí)現(xiàn)文件上傳和接收功能

admin
2025年7月22日 18:6 本文熱度 716

一、效果展示

postman測(cè)試
服務(wù)端接收
上傳成功
二、服務(wù)端代碼
引用
using System;using System.IO;using System.Web;using System.Web.Http;
WebApi接口
namespace Mes.Controllers{    public class FileController : ApiController    {        [HttpPost]        public string UploadFile()        {            string result = string.Empty;            try            {                //核心部分 文件                string fileName = HttpContext.Current.Request.Files[0].FileName;                Stream stream = HttpContext.Current.Request.Files[0].InputStream;
               //參數(shù)                var forms = HttpContext.Current.Request.Form;                foreach (var item in forms)                {                    string val = forms[item.ToString()];                }                //保存路徑                string uploadFolder = "F:\\圖片";                string savePath = System.IO.Path.Combine(uploadFolder, fileName);                FileStream fsWrite = new FileStream(savePath, FileMode.Create);
                byte[] bytes = new byte[1024 * 4];                int total = 0;                int size = 0;                do                {                    //注意第二個(gè)參數(shù)是在buffer中的偏移量,不是在文件中的偏移量                    size = stream.Read(bytes, 0, bytes.Length);                    fsWrite.Write(bytes, 0, size);                    total += size;                }                while (size > 0);
                fsWrite.Close();                result = "true";            }            catch (Exception ex)            {                result = ex.Message;            }            return result;        }    }}
三、客戶端代碼
using System.Net;using System.Net.Http;using System.Net.Http.Headers;
上傳文件方式一
/// <summary>/// 上傳文件,通過(guò)表單(multipart/form-data)方式一/// </summary>/// <param name="url"></param>/// <param name="dics"></param>/// <param name="filePath"></param>/// <returns></returns>public string PostFile(string url, Dictionary<stringobject> dics, string filePath){// 返回結(jié)果  string result = string.Empty;try	{//實(shí)例化multipart表單模型		MultipartFormDataContent form = new MultipartFormDataContent();using (HttpClient client = new HttpClient())		{			MediaTypeWithQualityHeaderValue temp = new MediaTypeWithQualityHeaderValue("application/json") { CharSet = "utf-8" };			client.DefaultRequestHeaders.Accept.Add(temp);//設(shè)定要響應(yīng)的數(shù)據(jù)格式
//參數(shù)foreach (var keyValuePair in dics) {// 設(shè)定文本類型表單項(xiàng),使用StringContent存放字符串 form.Add(new StringContent(keyValuePair.Value.ToString()), keyValuePair.Key); }
//讀取文件 Stream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);var fileName = Path.GetFileName(filePath);
//設(shè)定文件類型表單項(xiàng),使用StreamContent存放文件流 form.Add(new StreamContent(fileStream), "file", fileName);
Task<HttpResponseMessage> task = client.PostAsync(url, form); task.Wait(); // 等待異步操作完成 HttpResponseMessage response = task.Result; response.EnsureSuccessStatusCode(); // 確保響應(yīng)狀態(tài)碼表示成功
string responseBody = task.Result.Content.ReadAsStringAsync().Result; Console.WriteLine(responseBody); result = responseBody; }return result; }catch (Exception ex) {throw new Exception(ex.Message); }}
方式二
/// <summary>/// 上傳文件,通過(guò)表單(multipart/form-data)方式二/// </summary>/// <param name="url"></param>/// <param name="dics"></param>/// <param name="filePath"></param>/// <returns></returns>public string PostFile(string url, Dictionary<stringobject> dics, string filePath){// 返回結(jié)果  string result = string.Empty;try	{//實(shí)例化multipart表單模型		MultipartFormDataContent form = new MultipartFormDataContent();using (HttpClient client = new HttpClient())		{			MediaTypeWithQualityHeaderValue temp = new MediaTypeWithQualityHeaderValue("application/json") { CharSet = "utf-8" };			client.DefaultRequestHeaders.Accept.Add(temp);//設(shè)定要響應(yīng)的數(shù)據(jù)格式
//參數(shù)foreach (var keyValuePair in dics) {// 設(shè)定文本類型表單項(xiàng),使用StringContent存放字符串 form.Add(new StringContent(keyValuePair.Value.ToString()), keyValuePair.Key); }
//讀取文件 讀取為字節(jié)數(shù)據(jù)存入using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) {byte[] data = new byte[fileStream.Length]; fileStream.Read(data, 0, data.Length); fileStream.Close();
var fileName = Path.GetFileName(filePath); form.Add(new ByteArrayContent(data), "file", fileName); }
Task<HttpResponseMessage> task = client.PostAsync(url, form); task.Wait(); // 等待異步操作完成 HttpResponseMessage response = task.Result; response.EnsureSuccessStatusCode(); // 確保響應(yīng)狀態(tài)碼表示成功
string responseBody = task.Result.Content.ReadAsStringAsync().Result; Console.WriteLine(responseBody); result = responseBody; }return result; }catch (Exception ex) {throw new Exception(ex.Message); }}


閱讀原文:原文鏈接


該文章在 2025/7/23 12:17:41 編輯過(guò)
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved