如何从php调用“asp.net api上传文件”

时间:2014-06-28 07:52:55

标签: php asp.net-mvc api

我使用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

1 个答案:

答案 0 :(得分:0)

对我来说,看起来你有一个php网站,并在网上查找上传示例,并找到了一个asp.net。

如果你运行php为什么不使用php上传?