C# 如何将图像从PhoneGap上传到Asp.NETWebAPI?
我有以下可用的API:C# 如何将图像从PhoneGap上传到Asp.NETWebAPI?,c#,cordova,mime-types,file-transfer,C#,Cordova,Mime Types,File Transfer,我有以下可用的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("
[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
public class UserPhoto
{
public string Base64 { get; set; }
public byte[] Data { get; set; }
}
在后面的代码中,我尝试转换或获取每个请求数据的等效字节[]。如果我能得到正确的图像字节,那么我就可以开始了 在我的PhoneGap应用程序中,我有以下代码: 打开相机的功能:
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
}
);
}
我的第一次尝试是将图像转换为base64字符串并使用“up”API。对于低
质量
不高于50
的情况,它可以正常工作。但这幅图像几乎无法辨认。因此,我将质量设置为100
。然后新的问题来了,电话挂断了
因此,我尝试使用文件传输。代码如下:
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);
}
示例用法:
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;
}
但我无法上传文件,我总是得到不受支持的媒体类型格式化程序,有时出现空引用异常。我完全不知道。好吧,我现在明白了。对于其他在同一问题上苦苦挣扎的人来说,以下是解决方案
[HttpPost]
[CorsEnabled]
[ActionName("upfile")]
public DTO.Callback UploadPhoto()
{
var m = new Logic.Components.User();
return m.UploadPhoto(HttpContext.Current.Request, Common.UserValues().Email);
}
一些逻辑:
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." };
}
有关解决方案的一些说明:
第一部分是API,第二部分是后端代码
要将图像流从PhoneGap
传递到您的ASP.Net MVC Web API
,您只需使用HttpContext.Current.Request
从PhoneGap获取流