我使用asp.net创建了一个serive api,并使用POST方法上传了一个任意文件,我希望使用代码php调用该api,但我不知道如何调用。
Code asp.net接口上传文件如下:
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!"));
}
}和class:
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
发布于 2014-06-28 16:28:09
在我看来,你有一个php网站,在网上寻找一个上传示例,然后找到了一个asp.net网站。
如果你正在运行php,为什么不使用php进行上传呢?
https://stackoverflow.com/questions/24464816
复制相似问题