Web服务器如何接收Android发送过来的文件信息
Android 端代码:File file = new File(pathToOurFile);
MultipartEntity mpEntity = new MultipartEntity(); //文件传输
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("fileContent", cbFile);
请问ASP.Net如何接收他这样传来的信息??
--------------------编程问答-------------------- 这个应该是用到Webservice或WCF处理 --------------------编程问答-------------------- 服务器端提供Webservice接口 --------------------编程问答-------------------- 直接用 http handler 或者 http webapi 处理 multipart-data 请求
java:
File file = new File("F:\\tmp\\易做图ing\\conf-1.json");
MultipartEntity mpEntity = new MultipartEntity(); // 文件传输
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("fileContent", cbFile);
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("http://localhost:9999/api/values");
post.setEntity(mpEntity);
try {
CloseableHttpResponse response = client.execute(post);
String result = IOUtils.toString(response.getEntity().getContent());
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
.net http webapi
--------------------编程问答-------------------- Request.Files --------------------编程问答-------------------- 怎么感觉传过来的是一个文本
public HttpResponseMessage Post()
{
var content = Request.Content;
var uploadDir = HttpContext.Current.Server.MapPath("~/Upload");
var newFileName = "";
var sp = new MultipartMemoryStreamProvider();
Task.Run(async () => await Request.Content.ReadAsMultipartAsync(sp)).Wait();
foreach (var item in sp.Contents)
{
if (item.Headers.ContentDisposition.FileName != null)
{
var filename = item.Headers.ContentDisposition.FileName.Replace("\"", "");
newFileName = uploadDir + "\\" + filename;
var ms = item.ReadAsStreamAsync().Result;
using (var br = new BinaryReader(ms))
{
var data = br.ReadBytes((int)ms.Length);
File.WriteAllBytes(newFileName, data);
}
}
}
var result = new Dictionary<string, string>();
result.Add("result", newFileName);
var resp = Request.CreateResponse(HttpStatusCode.OK, result);
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
return resp;
}
补充:.NET技术 , Web Services