[英]How to upload image from PhoneGap to Asp.Net Web API?
I have these available APIs: 我有以下可用的API:
[HttpPost]
[CorsEnabled]
[ActionName("upstream")]
public DTO.Callback UploadPhoto(Stream data)
{
var m = new Logic.Components.User();
return m.UploadPhoto(data, Common.UserValues().Email);
}
[HttpPost]
[CorsEnabled]
[ActionName("upbyte")]
public DTO.Callback UploadPhoto(byte[] data)
{
var m = new Logic.Components.User();
return m.UploadPhoto(data, Common.UserValues().Email);
}
[HttpPost]
[CorsEnabled]
[ActionName("upfile")]
public DTO.Callback UploadPhoto(HttpPostedFileBase data)
{
var m = new Logic.Components.User();
return m.UploadPhoto(data, Common.UserValues().Email);
}
[HttpPost]
[CorsEnabled]
[ActionName("up")]
public DTO.Callback UploadPhoto(DTO.UserPhoto data)
{
var m = new Logic.Components.User();
return m.UploadPhoto(data, Common.UserValues().Email);
}
UserPhoto
class UserPhoto
类
public class UserPhoto
{
public string Base64 { get; set; }
public byte[] Data { get; set; }
}
In the behind code, I try to convert or get the equivalent byte[] of each request data. 在后面的代码中,我尝试转换或获取每个请求数据的等效byte []。
If I would get the correct Image byte, then I'm good to go. 如果我能得到正确的Image字节,那我很好。
In my PhoneGap application, I have these codes: 在我的PhoneGap应用程序中,我有以下代码:
A function that opens the camera: 打开相机的功能:
takePicture: function (success, error) {
var s = function (data) {
navigator.camera.cleanup();
(success || angular.noop)(data);
},
e = function (data) {
navigator.camera.cleanup();
(error || angular.noop)(data);
};
navigator.camera.getPicture(s, e,
{
quality: 100,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.CAMERA,
encodingType: Camera.EncodingType.PNG,
correctOrientation: true
}
);
}
My first try is to convert the image to base64 string and use the 'up' API. 我的第一个尝试是将图像转换为base64字符串,并使用“ up” API。
It works just fine for low quality
not higher than 50
. 对于不超过50
低quality
它工作得很好。 But the image becomes almost unrecognizable. 但是图像几乎变得无法识别。 So I set the quality
to 100
. 所以我将quality
设置为100
。 And then the new problem comes, the phone hangs... 然后新问题来了,电话挂了...
So I tried to use FileTransfer
. 所以我尝试使用FileTransfer
。 Here is the code: 这是代码:
fileTransfer: function (filePath, serverUri, mimeType, params, success, error) {
var
u = $coreData.user.getSession()
;
var options = new FileUploadOptions();
options.fileKey = 'file';
options.mimeType = mimeType;
options.params = params;
options.chunkedMode = true;
options.headers = {
Connection: 'close',
Device: m.createDeviceHeader(),
'Authentication-Token': (u && u.SessionKey),
'Content-Type': 'application/json'
};
var ft = new FileTransfer();
ft.upload(filePath, encodeURI(serverUri), success, error, options);
}
Sample usage: 用法示例:
uploadFile: function (path) {
var def = $q.defer();
$coreUtility
.fileTransfer(path, $coreAPI.user.getUrl('upfile'), 'image/png', null,
function (success) {
def.resolve(m.callback(true, UPLOAD_PHOTO_SUCCESS, success));
},
function (error) {
def.reject(m.callback(false, UPLOAD_PHOTO_FAIL, error));
});
return def.promise;
}
But I was not able to upload the file, I always get not supported media type formatter and sometimes null reference exceptions. 但是我无法上传文件,总是得到不支持的媒体类型格式化程序,有时还会出现null引用异常。 I'm totally out of idea. 我完全不知道
Alright I get it now. 好吧,我明白了。 For other struggling on the same problem, here is the solution. 对于其他在同一问题上苦苦挣扎的人,这里是解决方案。
[HttpPost]
[CorsEnabled]
[ActionName("upfile")]
public DTO.Callback UploadPhoto()
{
var m = new Logic.Components.User();
return m.UploadPhoto(HttpContext.Current.Request, Common.UserValues().Email);
}
Some logic: 一些逻辑:
public DTO.Callback UploadPhoto(HttpRequest req, string email)
{
if (req.Files.Count > 0)
{
var file = req.Files[0];
var m = new Logic.Components.User();
return m.UploadPhoto(file.InputStream, email);
}
return new DTO.Callback { Message = "Fail to upload. Make sure that you are uploading an image file." };
}
Some explanation about the solution: 有关解决方案的一些解释:
The first part is your API, and the second part is the backend code. 第一部分是您的API,第二部分是后端代码。
To pass the image stream from PhoneGap
to your ASP.Net MVC Web API
, you will just have to use HttpContext.Current.Request
to get the Stream
from Phonegap. 要将图像流从PhoneGap
传递到ASP.Net MVC Web API
,只需使用HttpContext.Current.Request
从Phonegap获取Stream
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.