webapi实现AJAX多文件上传,WebApi实现多文件上传

WebApi实现多文件上传

Api

#region UploadScanPage

///

/// Post

///

///

[HttpPost]

[Route("api/scn/UploadScanPage")]

[ResponseType(typeof(Resource))]

public async Task Post()

{

var resources = new List();

// multipart/form-data

// 采用MultipartMemoryStreamProvider

var provider = new MultipartMemoryStreamProvider();

// 读取文件数据

await Request.Content.ReadAsMultipartAsync(provider);

foreach (var item in provider.Contents)

{

// 判断是否是文件

if (item.Headers.ContentDisposition.FileName == null) continue;

// 获取到流

var ms = item.ReadAsStreamAsync().Result;

// 进行流操作

using (var br = new BinaryReader(ms))

{

if (ms.Length <= 0)

break;

// 读取文件内容到内存中

var data = br.ReadBytes((int) ms.Length);

/*

// Create

// 当前时间作为ID

var resource = new Resource()

{

Id = DateTime.Now.ToString("yyyyMMddHHmmssffff", DateTimeFormatInfo.InvariantInfo)

};

*/

// Info

var info = new FileInfo(item.Headers.ContentDisposition.FileName.Replace("\"", ""));

var resource = new Resource

{

Id = info.Name,

Type = info.Extension.Substring(1).ToLower()

};

// 文件类型

// Write

try

{

// 文件存储地址

var dirPath = Path.Combine(RootPath);

if (!Directory.Exists(dirPath))

{

Directory.CreateDirectory(dirPath);

}

File.WriteAllBytes(Path.Combine(dirPath, resource.Id), data);

resources.Add(resource);

}

catch

{

// ignored

}

}

}

// 返回

switch (resources.Count)

{

case 0:

return BadRequest();

case 1:

return Ok(resources.FirstOrDefault());

default:

return Ok(resources);

}

}

#endregion

Resource

public class Resource

{

public string Id { get; set; }

public string Type { get; set; }

}

UpLoadFileTest

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Net;

using System.Net.Http;

using System.Net.Http.Headers;

using System.Text;

using System.Threading.Tasks;

namespace UpLoadFileTest

{

public class UpLoadFileTest

{

///

/// BatchUpLoadFiles

///

///

///

///

///

public void BatchUpLoadFiles(string baseAddress, string postUrl

,string imagesPath, List files)

{

/*

* 批量上传图片

* baseAddress: "http://localhost:7427/"

* postUrl: "/api/scn/UploadScanPage"

* imagesPath: "e:\10001003\"

* filePath: "e:\10001003\1.jpg"

*/

using (var client = new HttpClient())

{

using (var content = new MultipartFormDataContent())

{

// Make sure to change API address

client.BaseAddress = new Uri(baseAddress);

var count = 1;

foreach (var imageName in files)

{

// Add file content

var filePath = imagesPath + imageName;

var fileContent = new ByteArrayContent(File.ReadAllBytes(@filePath));

fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")

{

FileName = imageName

};

content.Add(fileContent);

Console.WriteLine((count++) + "/" + files.Count);

}

// Make a call to Web API

var result = client.PostAsync(postUrl, content).Result;

Console.WriteLine(result.StatusCode);

Console.ReadLine();

}

}

}

}

}

Program

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Net.Http;

using System.Net.Http.Headers;

using System.Text;

using System.Threading.Tasks;

namespace UpLoadFileTest

{

class Program

{

static void Main(string[] args)

{

var baseAddress = "http://localhost:7427/";

var postUrl = "/api/scn/UploadScanPage";

var imagesPath = @"e:\10001003\";

var files = new List {"1.jpg", "2.jpg"};

new UpLoadFileTest().BatchUpLoadFiles(baseAddress, postUrl, imagesPath, files);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值