首先定义公共的返回对象

/// <summary>
/// 返回数据对象
/// </summary>
public class ResponseItem<T>
{
    public Boolean success { get; set; }
    public String msg { get; set; }
    public T data { get; set; }

    public ResponseItem()
    {

    }

    public ResponseItem(Boolean success,String msg,T data)
    {
        this.success = success;
        this.msg = msg;
        this.data = data;
    }
}

其次是所有的api接口都必须继承自ApiController。

文件上传的代码如下:

[HttpPost]
public String SaveFile()
{
    if (Request.Content.IsMimeMultipartContent())
    {
        Request.Content.ReadAsMultipartAsync().ContinueWith(p =>
        {
            foreach (var item in p.Result.Contents)
            {
                if (String.IsNullOrEmpty(item.Headers.ContentDisposition.FileName))
                {
                    continue;
                }

                item.ReadAsStreamAsync().ContinueWith(a =>
                {
                    Stream stream = a.Result;
                    String fileName = item.Headers.ContentDisposition.FileName;
                    fileName = fileName.Substring(1, fileName.Length - 2);
                    //保存
                    byte[] r = new byte[stream.Length];
                    stream.Read(r, 0, r.Length);

                    File.WriteAllBytes(Path.Combine("E:", fileName), r);

                });
            }
        });
    }
    return "1";
}
View Code

相关文章:

  • 2022-02-09
  • 2021-12-02
  • 2022-12-23
  • 2021-11-19
  • 2021-10-20
  • 2021-07-26
  • 2022-12-23
  • 2021-12-18
猜你喜欢
  • 2021-08-21
  • 2022-12-23
  • 2021-07-09
  • 2021-12-02
  • 2021-10-23
  • 2022-02-11
  • 2022-12-23
相关资源
相似解决方案