標籤: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(); } }}
Ionic app 上傳圖片之webApi介面