@@ -1899,7 +1899,7 @@ class Component {
1899
1899
const thisPromiseResolve = this . nextRequestPromiseResolve ;
1900
1900
this . resetPromise ( ) ;
1901
1901
this . unsyncedInputsTracker . resetUnsyncedFields ( ) ;
1902
- this . backendRequest = this . backend . makeRequest ( this . valueStore . getOriginalProps ( ) , this . pendingActions , this . valueStore . getDirtyProps ( ) , this . getChildrenFingerprints ( ) , this . valueStore . getUpdatedPropsFromParent ( ) ) ;
1902
+ this . backendRequest = this . backend . makeRequest ( this . valueStore . getOriginalProps ( ) , this . pendingActions , this . valueStore . getDirtyProps ( ) , this . getChildrenFingerprints ( ) , this . valueStore . getUpdatedPropsFromParent ( ) , { } ) ;
1903
1903
this . hooks . triggerHook ( 'loading.state:started' , this . element , this . backendRequest ) ;
1904
1904
this . pendingActions = [ ] ;
1905
1905
this . valueStore . flushDirtyPropsToPending ( ) ;
@@ -2129,7 +2129,7 @@ class RequestBuilder {
2129
2129
this . url = url ;
2130
2130
this . csrfToken = csrfToken ;
2131
2131
}
2132
- buildRequest ( props , actions , updated , children , updatedPropsFromParent ) {
2132
+ buildRequest ( props , actions , updated , children , updatedPropsFromParent , files ) {
2133
2133
const splitUrl = this . url . split ( '?' ) ;
2134
2134
let [ url ] = splitUrl ;
2135
2135
const [ , queryString ] = splitUrl ;
@@ -2138,8 +2138,10 @@ class RequestBuilder {
2138
2138
fetchOptions . headers = {
2139
2139
Accept : 'application/vnd.live-component+html' ,
2140
2140
} ;
2141
+ const totalFiles = Object . entries ( files ) . reduce ( ( total , current ) => total + current . length , 0 ) ;
2141
2142
const hasFingerprints = Object . keys ( children ) . length > 0 ;
2142
2143
if ( actions . length === 0 &&
2144
+ totalFiles === 0 &&
2143
2145
this . willDataFitInUrl ( JSON . stringify ( props ) , JSON . stringify ( updated ) , params , JSON . stringify ( children ) , JSON . stringify ( updatedPropsFromParent ) ) ) {
2144
2146
params . set ( 'props' , JSON . stringify ( props ) ) ;
2145
2147
params . set ( 'updated' , JSON . stringify ( updated ) ) ;
@@ -2153,18 +2155,18 @@ class RequestBuilder {
2153
2155
}
2154
2156
else {
2155
2157
fetchOptions . method = 'POST' ;
2156
- fetchOptions . headers [ 'Content-Type' ] = 'application/json' ;
2157
2158
const requestData = { props, updated } ;
2158
2159
if ( Object . keys ( updatedPropsFromParent ) . length > 0 ) {
2159
2160
requestData . propsFromParent = updatedPropsFromParent ;
2160
2161
}
2161
2162
if ( hasFingerprints ) {
2162
2163
requestData . children = children ;
2163
2164
}
2165
+ if ( this . csrfToken &&
2166
+ ( actions . length || totalFiles ) ) {
2167
+ fetchOptions . headers [ 'X-CSRF-TOKEN' ] = this . csrfToken ;
2168
+ }
2164
2169
if ( actions . length > 0 ) {
2165
- if ( this . csrfToken ) {
2166
- fetchOptions . headers [ 'X-CSRF-TOKEN' ] = this . csrfToken ;
2167
- }
2168
2170
if ( actions . length === 1 ) {
2169
2171
requestData . args = actions [ 0 ] . args ;
2170
2172
url += `/${ encodeURIComponent ( actions [ 0 ] . name ) } ` ;
@@ -2174,7 +2176,15 @@ class RequestBuilder {
2174
2176
requestData . actions = actions ;
2175
2177
}
2176
2178
}
2177
- fetchOptions . body = JSON . stringify ( requestData ) ;
2179
+ const formData = new FormData ( ) ;
2180
+ formData . append ( 'data' , JSON . stringify ( requestData ) ) ;
2181
+ for ( const [ key , value ] of Object . entries ( files ) ) {
2182
+ const length = value . length ;
2183
+ for ( let i = 0 ; i < length ; ++ i ) {
2184
+ formData . append ( key , value [ i ] ) ;
2185
+ }
2186
+ }
2187
+ fetchOptions . body = formData ;
2178
2188
}
2179
2189
const paramsString = params . toString ( ) ;
2180
2190
return {
@@ -2192,8 +2202,8 @@ class Backend {
2192
2202
constructor ( url , csrfToken = null ) {
2193
2203
this . requestBuilder = new RequestBuilder ( url , csrfToken ) ;
2194
2204
}
2195
- makeRequest ( props , actions , updated , children , updatedPropsFromParent ) {
2196
- const { url, fetchOptions } = this . requestBuilder . buildRequest ( props , actions , updated , children , updatedPropsFromParent ) ;
2205
+ makeRequest ( props , actions , updated , children , updatedPropsFromParent , files ) {
2206
+ const { url, fetchOptions } = this . requestBuilder . buildRequest ( props , actions , updated , children , updatedPropsFromParent , files ) ;
2197
2207
return new BackendRequest ( fetch ( url , fetchOptions ) , actions . map ( ( backendAction ) => backendAction . name ) , Object . keys ( updated ) ) ;
2198
2208
}
2199
2209
}
0 commit comments