码迷,mamicode.com
首页 > 移动开发 > 详细

Ionic app 上传图片之webApi接口

时间:2016-11-07 17:37:49      阅读:259      评论:0      收藏:0      [点我收藏+]

标签: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接口

标签:address   ace   图片   get   filename   return   names   测试   partial   

原文地址:http://www.cnblogs.com/crazyguo/p/6039759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!