zoukankan      html  css  js  c++  java
  • 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:mailmarathon.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();
            }
        }
    }
  • 相关阅读:
    面试知识点连接汇总:
    spring学习二:jdbc相关回顾以及spring下dao
    spring学习一:spring入门及相关概念介绍
    日常暖手
    日常暖手
    从破解实例到探讨反调试
    一个有趣的CM
    复健小CM
    Windows下利用py2exe生成静默运行的命令行程序
    获取指定窗口内的文本
  • 原文地址:https://www.cnblogs.com/crazyguo/p/6039759.html
Copyright © 2011-2022 走看看