从Xamarin C#app

时间:2016-03-10 17:14:04

标签: javascript c# node.js xamarin multer

我试图从PCL Xamarin表单库将图像上传到Node JS。我尝试使用Multer Node JS库来获取POST请求。收到请求,但照片未保存。该文件始终未定义'。

节点JS代码:

var formidable = require('formidable');
var util = require('util');
var fs = require('fs');
var multer = require('multer');

var storage = multer.diskStorage({
    destination: function (req, file, callback) {
        callback(null, './photouploads');
    },
    filename: function (req, file, callback) {
        console.log(file.fieldname);
        callback(null, file.fieldname + '-' + Date.now());
    }
});

var upload = multer({ storage: storage }).single('image');

exports.post = function (req, res) {
    console.log("Photo Api Hit");
    upload(req, res, function (err) {
        console.log(req.file);
        if (err) {
            console.log("error");
            console.log(err);
            return res.end("Error uploading file.");
        }
        console.log("File has been received");
        res.end("File is uploaded");
    });
};

C#/ Xamarin代码

using Plugin.Media.Abstractions;
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;

namespace Vigour.Controllers
{
    class MediaController
    {
        public static void upload(MediaFile mediaFile)
        {
            try
            {
                StreamContent scontent = new StreamContent(mediaFile.GetStream());
                scontent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                { 
                    FileName = "newimage",
                    Name = "image"
                };
                scontent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
                var client = new HttpClient();
                client.BaseAddress = new Uri(Constants.API_ROOT_URL);
                var result = client.PostAsync("api/photo", scontent).Result;
                Debug.WriteLine(result.ReasonPhrase);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
        }
}
}

有谁知道应该如何在我的C#代码中形成请求?感谢

1 个答案:

答案 0 :(得分:0)

我发现使用这种方法对PCL有效

MediaFile是Xamarin.Plugins.Media Library

中的对象
 public static void upload(MediaFile mediaFile)
 {
            try
            {
                StreamContent scontent = new StreamContent(mediaFile.GetStream());
                scontent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                {
                    FileName = "newimage",
                    Name = "image"
                };
                scontent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");

                var client = new HttpClient();
                var multi = new MultipartFormDataContent();
                multi.Add(scontent);
                client.BaseAddress = new Uri(Constants.API_ROOT_URL);
                var result = client.PostAsync("api/photo", multi).Result;
                Debug.WriteLine(result.ReasonPhrase);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
  }

我使用节点js(托管在azure上)来接收请求。

var formidable = require('formidable');
var util = require('util');
var fs = require('fs');
var multer = require('multer');

var storage = multer.diskStorage({
    destination: function (req, file, callback) {
        callback(null, './uploads');
    },
    filename: function (req, file, callback) {
        console.log(file.fieldname);
        callback(null, file.fieldname + '-' + Date.now());
    }
});

var upload = multer({ storage: storage }).single('image');

exports.post = function (req, res) {
    console.log("Photo Api Hit");
    upload(req, res, function (err) {
        console.log(req.file);
        if (err) {
            console.log("Photo API ERROR: "+err);
            return res.end("Error uploading file.");
        }
        console.log("SUCCESS");
        res.end("File is uploaded");
    });
};