首页 > 代码库 > Ionic app 上传图片之webApi接口
Ionic app 上传图片之webApi接口
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(); } } }
Ionic app 上传图片之webApi接口
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。