-
-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to upload file on iOS? #92
Comments
What do you mean by
Is that error coming from the |
I mean it's not encoded in such format:
I haven't found any way to use it to encode form with file and just gave up and spin up own solution. |
|
Sorry, I meant "filename" part. Basically was trying to use this library to make a request with multipart form-data body containing both regular key - values and a file within. So in the end it would look like:
But I have not found a way to encode file using it. Does it make sense? |
Yes it does make sense. Have you tried |
I am not sure what are you referring to. There is no such thing as |
Sorry it's a Vapor thing https://github.com/vapor/vapor/blob/main/Sources/Vapor/Utilities/File.swift If you're trying to use this outside of Vapor you can copy and paste it and I think it will work |
When using
However, it seems like @pzmudzinski is expecting it to be encoded like this:
I also agree with this, and it appears that without this format, some runtimes may not recognize the file as such. |
Sorry, I had missed the extension in Vapor that addresses this issue. |
@pzmudzinski Here is the simple import Foundation
import MultipartKit
import NIOFoundationCompat
public struct File: Codable, Equatable, Sendable, MultipartPartConvertible {
public var filename: String
public var data: Data
public var contentType: String?
public init(data: Data, filename: String, contentType: String? = nil) {
self.data = data
self.filename = filename
self.contentType = contentType
}
// MARK: - MultipartPartConvertible
public var multipart: MultipartPart? {
var part = MultipartPart(headers: [:], body: data)
if let contentType {
part.headers.replaceOrAdd(name: "Content-Type", value: contentType)
}
part.headers.replaceOrAdd(
name: "Content-Disposition",
value: !filename.contains("\"")
? "form-data; filename=\"\(filename)\""
: "form-data; filename='\(filename)'"
)
return part
}
public init?(multipart: MultipartPart) {
let filenameRegex = /filename=(?:"([^"]+)"|'([^']+)'|([^\s"';]+))/
guard let contentDisposition = multipart.headers.first(name: "Content-Disposition"),
let output = contentDisposition.firstMatch(of: filenameRegex)?.output,
let filename = output.1 ?? output.2 ?? output.3
else {
return nil
}
self.init(
data: Data(buffer: multipart.body),
filename: String(filename),
contentType: multipart.headers.first(name: "Content-Type")
)
}
} |
@pzmudzinski Does this solve your issue? |
I was trying to encode struct like that:
But
imageFile
is not recognized asfile
part. Also I could not find any example.Also sorry for bug label - don't know how to remove it 😅
The text was updated successfully, but these errors were encountered: