【问题标题】:How to call "asp.net api upload file" from php如何从php调用“asp.net api上传文件”
【发布时间】:2014-08-19 07:56:09
【问题描述】:

我使用 asp.net 创建一个服务 api,并使用方法 POST 上传任何文件,我想使用代码 php 调用该 api,但我不知道如何。

代码asp.net api上传文件如下:

public Task<IEnumerable<string>> Post()
    {
        if (Request.Content.IsMimeMultipartContent())
        {
            string fullPath = HttpContext.Current.Server.MapPath("~/uploads");
            MyMultipartFormDataStreamProvider streamProvider = new MyMultipartFormDataStreamProvider(fullPath);
            var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>
            {
                if (t.IsFaulted || t.IsCanceled)
                    throw new HttpResponseException(HttpStatusCode.InternalServerError);

                var fileInfo = streamProvider.FileData.Select(i =>
                {
                    var info = new FileInfo(i.LocalFileName);
                    return "File uploaded as " + info.FullName + " (" + info.Length + ")";
                });
                return fileInfo;

            });
            return task;
        }
        else
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "Invalid Request!"));
        }
    }

和类:

public class MyMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
{
    public MyMultipartFormDataStreamProvider(string path)
        : base(path)
    {

    }

    public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers)
    {
        string fileName;
        if (!string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName))
        {
            fileName = headers.ContentDisposition.FileName;
        }
        else
        {
            fileName = Guid.NewGuid().ToString() + ".data";
        }
        return fileName.Replace("\"", string.Empty);
    }
}

那么,我如何使用代码 php 调用这个 api 呢?

我将代码引用到:http://www.codeguru.com/csharp/.net/uploading-files-asynchronously-using-asp.net-web-api.htm

【问题讨论】:

    标签: php asp.net-mvc api


    【解决方案1】:

    在我看来,您似乎有一个 php 网站,并在网上查看了上传示例并找到了一个 asp.net。

    如果您运行 php,为什么不使用 php 进行上传?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-05
      • 2020-04-08
      • 2022-12-14
      • 2020-07-10
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多