我正在尝试将带有参数的多个文件上传到ASP.NET方法,ASP.NET方法期望一个类的列表,我能够发送ASP.NET期望的东西,但是我的HttpPostedFile为null: (我的问题是我在iOS方面做错了什么?这是我用Swift编写的iOS函数:
func saveQAPhotos(_ cellHolder: Array<PhotoClass>, completion: @escaping (_ result: String) -> Void)
{
//Define Array of Dictionary
var jsonArrayOfDictionaries = [[AnyHashable: Any]]()
//For each item in the cellHolder
for i in 0..<cellHolder.count {
//Define Dictionary for grading data
var jsonDict = [AnyHashable: Any]()
jsonDict["job"] = cellHolder[i].job
jsonDict["imageBytes"] = cellHolder[i].photo!.base64EncodedString().addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
jsonDict["createdBy"] = appDelegate.username
jsonDict["itemId"] = cellHolder[i].itemId
jsonArrayOfDictionaries.append(jsonDict)
jsonDict = [AnyHashable: Any]()
}
let jsonData: Data? = try? JSONSerialization.data(withJSONObject: jsonArrayOfDictionaries, options: .prettyPrinted)
let urlComponents = NSURLComponents(string: webservice + "uploadQAImage");
urlComponents?.user = appDelegate.username;
urlComponents?.password = appDelegate.password;
let url = urlComponents?.url;
var request = URLRequest(url: url!)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("multipart/form-data", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = jsonData
URLSession.shared.dataTask(with: request, completionHandler: {
(data, response, error) in
if(error != nil){
completion((error?.localizedDescription)!)
}else{
let responseString = String(data: data!, encoding: .utf8)
OperationQueue.main.addOperation({
completion(responseString!)
})
}
}).resume()
}
这是我的一些ASP.NET方法:
public string uploadQAImage(List<FileUploadClass> fileUploads) {
for (int i = 0; i < fileUploads.Count; i++) {
HttpPostedFile hfc = fileUploads[i].imageBytes;
}
}
当我将相关文件的iOS数据发送给hfc时,它始终为null。我的应用程序上确实安装了Alamofire,可以使用它,但是可以使用上载方法发送ASP.NET期望的内容吗?
这是我的模特
public class FileUploadClass
{
public string job { get; set; }
public string createdBy { get; set; }
public HttpPostedFile imageBytes { get; set; }
public int itemId { get; set; }
}
这是我的速成班
class PhotoClass: NSObject {
var job: String?
var photo: Data?
var itemId: Int?
init(job: String?, photo: Data, itemId: Int?)
{
self.job = job
self.photo = photo
self.itemId = itemId
}
}
答案 0 :(得分:0)
根据to the documentation,预期的格式为multipart/form-data
的MIME HttpPostedFile
:
文件以MIME multipart / form-data格式上载。默认情况下,所有 大于256 KB的请求,包括表单字段和上载的文件 被缓冲到磁盘,而不是保存在服务器内存中。
您正在发送某种JSON格式的POST正文并将其称为多部分,但是实际上您需要发送多部分的MIME。描述多部分MIME格式的RFC在这里:https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
不幸的是,我没有使用过许多可以为您进行这种编码的库。这个看起来好用:https://github.com/Fyrts/Multipart-这是一个示例:
import Multipart
let fileContents = try! Data(contentsOf: URL(string: "/Users/user/Desktop/document.pdf")!)
var message = Multipart(type: .formData)
message.append(Part.FormData(name: "message", value: "See attached file."))
message.append(Part.FormData(name: "file", fileData: fileContents, fileName: "document.pdf", contentType: "application/pdf"))
var request = URLRequest(url: URL(string: "https://example.com")!)
request.httpMethod = "POST"
request.setMultipartBody(message)
URLSession.shared.dataTask(with: request) { data, response, error in
print(data, response, error)
}.resume()