我在客户端和alamofire上使用swift 2将图像和参数发送到我的服务器即web api 2 C# 客户端代码
// init paramters Dictionary
var parameters = [
"OwnerUserId": UserId!,
"GroupId": "nb",
"GroupName": groupName!
]
// example image data
let image = UIImage(named: "IMG_0801.PNG")
let imageData = UIImagePNGRepresentation(image!)
// CREATE AND SEND REQUEST ----------
let urlRequest = urlRequestWithComponents("http://xyz.azurewebsites.net:80/api/files", parameters: parameters, imageData: imageData!)
Alamofire.upload(urlRequest.0, data: urlRequest.1)
.progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
print("\(totalBytesWritten) / \(totalBytesExpectedToWrite)")
}
.responseJSON { response in
print("RESPONSE \(response)")
}
并且多部分植入是
// this function creates the required URLRequestConvertible and NSData we need to use Alamofire.upload
func urlRequestWithComponents(urlString:String, parameters:Dictionary<String, String>, imageData:NSData) -> (URLRequestConvertible, NSData)
{
// create url request to send
let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: urlString)!)
mutableURLRequest.HTTPMethod = Alamofire.Method.POST.rawValue
let boundaryConstant = "myRandomBoundary12345";
let contentType = "multipart/form-data;boundary="+boundaryConstant
mutableURLRequest.setValue(contentType, forHTTPHeaderField: "Content-Type")
// create upload data to send
let uploadData = NSMutableData()
// add image
uploadData.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
uploadData.appendData("Content-Disposition: form-data; name=\"file\"; filename=\"file.png\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
uploadData.appendData("Content-Type: image/png\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
uploadData.appendData(imageData)
// add parameters
for (key, value) in parameters {
uploadData.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
uploadData.appendData("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n\(value)".dataUsingEncoding(NSUTF8StringEncoding)!)
}
uploadData.appendData("\r\n--\(boundaryConstant)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
// return URLRequestConvertible and NSData
return (Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: nil).0, uploadData)
}
现在我将图像和参数发送到服务器,我收到了此错误
响应成功:{ 消息=&#34;请求实体的媒体类型&quot; multipart / form-data&#39;此资源不支持。&#34 ;; }
服务器代码是
[Route("api/files")]
[HttpPost]
public async Task<Collection<AzureBlobInfo>> Post([FromBody] Groups parametersToSend)
{
if (!Request.Content.IsMimeMultipartContent("form-data"))
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
// Get or create the blob container
CloudBlobContainer container = await _blobContainer.Value;
// Create a AzureBlobStorageMultipartProvider and process the request
AzureBlobStorageMultipartProvider streamProvider = new AzureBlobStorageMultipartProvider(container, Path.GetTempPath());
await Request.Content.ReadAsMultipartAsync<AzureBlobStorageMultipartProvider>(streamProvider);
// Return result from storing content in the blob container
return streamProvider.AzureBlobs;
}
行if (!Request.Content.IsMimeMultipartContent("form-data"))
是alaways返回true,我不明白为什么