标签:address ace 图片 get filename return names 测试 partial
App上传图片对应的webApi服务端是怎么处理的呢?
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using System.Web; using System.Web.Hosting; using System.Web.Http; namespace Ionic.App.WebApi.Controllers { public class UploadApiController : ApiController {public UploadApiController() { } [HttpPost] public Task<HttpResponseMessage> Post() { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } //获得查询字符串的键值集合 var queryp = Request.GetQueryNameValuePairs(); string root = "C:\\inetpub\\wuzapp\\App_Data"; var provider = new MultipartFormDataStreamProvider(root); // Read the form data return Request.Content.ReadAsMultipartAsync(provider).ContinueWith(t => { string message = "Hello"; foreach (MultipartFileData file in provider.FileData) { Trace.WriteLine(file.Headers.ContentDisposition.FileName); Trace.WriteLine("Server file path: " + file.LocalFileName); if (File.Exists(file.LocalFileName)) { message = file.LocalFileName; //do some } else { message = file.LocalFileName + " no exist"; } } return Request.CreateResponse(HttpStatusCode.OK,message); }, TaskScheduler.FromCurrentSynchronizationContext()); } } }
其中红色的部分是写死了具体的保存文件的路径,在测试的时候发现在本机可以上传图片,换到服务器上,一个可以上传有文件,一个却没有,怀疑是权限问题。
给这个目录对应的everyone帐号添加read/write权限,果然可以上传成功。
这样文件/图片等内容就可以通过App上传了。
C# windForm端的测试代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Http; using System.IO; using System.Net.Http.Headers; namespace ApplicationUI { public partial class FileUploadForm : Form { public FileUploadForm() { InitializeComponent(); } public void Upload() { using (var client = new HttpClient()) using (var content = new MultipartFormDataContent()) {client.BaseAddress = new Uri("http://wuznt016/flexpsappapi/"); var file = @"d:\mail\marathon.txt"; var fileContent = new ByteArrayContent(File.ReadAllBytes(file)); fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "marathon.txt" }; content.Add(fileContent); var result = client.PostAsync("api/Upload/Post", content).Result; Console.WriteLine(result.StatusCode); } } private void btnUpload_Click(object sender, EventArgs e) { Upload(); } } }
标签:address ace 图片 get filename return names 测试 partial
原文地址:http://www.cnblogs.com/crazyguo/p/6039759.html