@@ -1120,29 +1120,47 @@ winrt::fire_and_forget RNFetchBlob::fetchBlob(
1120
1120
1121
1121
winrt::Windows::Web::Http::HttpRequestMessage requestMessage{ httpMethod, Uri{url} };
1122
1122
bool pathToFile{ body.rfind (prefix, 0 ) == 0 };
1123
- winrt::hstring fileContent;
1124
1123
if (pathToFile)
1125
1124
{
1126
1125
std::string contentPath{ body.substr (prefix.length ()) };
1127
- std::filesystem::path path{ contentPath };
1128
- path.make_preferred ();
1126
+ size_t fileLength = contentPath.length ();
1127
+ bool hasTrailingSlash{ contentPath[fileLength - 1 ] == ' \\ ' || contentPath[fileLength - 1 ] == ' /' };
1128
+ winrt::hstring directoryPath, fileName;
1129
+ splitPath (hasTrailingSlash ? contentPath.substr (0 , fileLength - 1 ) : contentPath, directoryPath, fileName);
1130
+ StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync (directoryPath) };
1131
+ StorageFile storageFile{ co_await folder.CreateFileAsync (fileName, CreationCollisionOption::OpenIfExists) };
1132
+ IBuffer requestBuffer{ co_await FileIO::ReadBufferAsync (storageFile) };
1129
1133
1130
- StorageFile storageFile{ co_await StorageFile::GetFileFromPathAsync (winrt::to_hstring (path.c_str ())) };
1131
- fileContent = co_await FileIO::ReadTextAsync (storageFile);
1132
- }
1134
+ winrt::Windows::Web::Http::HttpBufferContent requestContent{ requestBuffer };
1133
1135
1134
- winrt::Windows::Web::Http::HttpStringContent requestContent{ pathToFile ? fileContent : winrt::to_hstring (body) };
1136
+ for (auto const & entry : headers)
1137
+ {
1138
+ if (!requestMessage.Headers ().TryAppendWithoutValidation (winrt::to_hstring (entry.first ), winrt::to_hstring (entry.second .AsString ())))
1139
+ {
1140
+ requestContent.Headers ().TryAppendWithoutValidation (winrt::to_hstring (entry.first ), winrt::to_hstring (entry.second .AsString ()));
1141
+ }
1142
+ }
1143
+ requestMessage.Content (requestContent);
1144
+ }
1145
+ else if (!body.empty ()) {
1146
+ winrt::Windows::Web::Http::HttpStringContent requestString{ winrt::to_hstring (body) };
1135
1147
1136
- for (auto const & entry : headers)
1137
- {
1138
- if (!requestMessage.Headers ().TryAppendWithoutValidation (winrt::to_hstring (entry.first ), winrt::to_hstring (entry.second .AsString ())))
1148
+ for (auto const & entry : headers)
1139
1149
{
1140
- requestContent.Headers ().TryAppendWithoutValidation (winrt::to_hstring (entry.first ), winrt::to_hstring (entry.second .AsString ()));
1150
+ if (!requestMessage.Headers ().TryAppendWithoutValidation (winrt::to_hstring (entry.first ), winrt::to_hstring (entry.second .AsString ())))
1151
+ {
1152
+ requestString.Headers ().TryAppendWithoutValidation (winrt::to_hstring (entry.first ), winrt::to_hstring (entry.second .AsString ()));
1153
+ }
1154
+ }
1155
+ requestMessage.Content (requestString);
1156
+ }
1157
+ else {
1158
+ for (auto const & entry : headers)
1159
+ {
1160
+ requestMessage.Headers ().TryAppendWithoutValidation (winrt::to_hstring (entry.first ), winrt::to_hstring (entry.second .AsString ()));
1141
1161
}
1142
1162
}
1143
1163
1144
- requestMessage.Content (requestContent);
1145
-
1146
1164
auto exists{ uploadProgressMap.find (taskId) };
1147
1165
if (exists != uploadProgressMap.end ()) {
1148
1166
auto progress{ uploadProgressMap[taskId] };
@@ -1280,35 +1298,55 @@ winrt::fire_and_forget RNFetchBlob::fetchBlobForm(
1280
1298
1281
1299
auto data{ items[" data" ].AsString () };
1282
1300
bool pathToFile{ data.rfind (prefix, 0 ) == 0 };
1283
- winrt::hstring fileContent;
1284
1301
if (pathToFile)
1285
1302
{
1286
1303
std::string contentPath{ data.substr (prefix.length ()) };
1287
- std::filesystem::path path{ contentPath };
1288
- path.make_preferred ();
1289
-
1290
- StorageFile storageFile{ co_await StorageFile::GetFileFromPathAsync (winrt::to_hstring (path.c_str ())) };
1291
- fileContent = co_await FileIO::ReadTextAsync (storageFile);
1292
-
1293
- }
1294
- winrt::Windows::Web::Http::HttpStringContent dataContents{ pathToFile ? fileContent : winrt::to_hstring (data) };
1295
- if (!items[" type" ].IsNull ()) {
1296
- dataContents.Headers ().TryAppendWithoutValidation (L" content-type" , winrt::to_hstring (items[" type" ].AsString ()));
1297
- }
1304
+ size_t fileLength = contentPath.length ();
1305
+ bool hasTrailingSlash{ contentPath[fileLength - 1 ] == ' \\ ' || contentPath[fileLength - 1 ] == ' /' };
1306
+ winrt::hstring directoryPath, fileName;
1307
+ splitPath (hasTrailingSlash ? contentPath.substr (0 , fileLength - 1 ) : contentPath, directoryPath, fileName);
1308
+ StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync (directoryPath) };
1309
+ StorageFile storageFile = co_await folder.CreateFileAsync (fileName, CreationCollisionOption::OpenIfExists);
1310
+ IBuffer requestBuffer{ co_await FileIO::ReadBufferAsync (storageFile) };
1311
+
1312
+ winrt::Windows::Web::Http::HttpBufferContent requestBufferContent{ requestBuffer };
1313
+
1314
+ if (!items[" type" ].IsNull ()) {
1315
+ requestBufferContent.Headers ().TryAppendWithoutValidation (L" content-type" , winrt::to_hstring (items[" type" ].AsString ()));
1316
+ }
1298
1317
1299
- auto name{ items[" name" ].IsNull () ? L" " : winrt::to_hstring (items[" name" ].AsString ()) };
1300
- if (name.size () <= 0 ) {
1301
- requestContent.Add (dataContents);
1302
- continue ;
1303
- }
1304
- auto filename{ items[" filename" ].IsNull () ? L" " : winrt::to_hstring (items[" filename" ].AsString ()) };
1305
- if (filename.size () <= 0 ) {
1306
- requestContent.Add (dataContents, name);
1318
+ auto name{ items[" name" ].IsNull () ? L" " : winrt::to_hstring (items[" name" ].AsString ()) };
1319
+ if (name.size () <= 0 ) {
1320
+ requestContent.Add (requestBufferContent);
1321
+ continue ;
1322
+ }
1323
+ auto filename{ items[" filename" ].IsNull () ? L" " : winrt::to_hstring (items[" filename" ].AsString ()) };
1324
+ if (filename.size () <= 0 ) {
1325
+ requestContent.Add (requestBufferContent, name);
1326
+ }
1327
+ else {
1328
+ requestContent.Add (requestBufferContent, name, filename);
1329
+ }
1307
1330
}
1308
1331
else {
1309
- requestContent.Add (dataContents, name, filename);
1310
- }
1332
+ winrt::Windows::Web::Http::HttpStringContent dataContents{ winrt::to_hstring (data) };
1333
+ if (!items[" type" ].IsNull ()) {
1334
+ dataContents.Headers ().TryAppendWithoutValidation (L" content-type" , winrt::to_hstring (items[" type" ].AsString ()));
1335
+ }
1311
1336
1337
+ auto name{ items[" name" ].IsNull () ? L" " : winrt::to_hstring (items[" name" ].AsString ()) };
1338
+ if (name.size () <= 0 ) {
1339
+ requestContent.Add (dataContents);
1340
+ continue ;
1341
+ }
1342
+ auto filename{ items[" filename" ].IsNull () ? L" " : winrt::to_hstring (items[" filename" ].AsString ()) };
1343
+ if (filename.size () <= 0 ) {
1344
+ requestContent.Add (dataContents, name);
1345
+ }
1346
+ else {
1347
+ requestContent.Add (dataContents, name, filename);
1348
+ }
1349
+ }
1312
1350
}
1313
1351
requestMessage.Content (requestContent);
1314
1352
@@ -1530,7 +1568,8 @@ try
1530
1568
for (;;)
1531
1569
{
1532
1570
buffer.Length (0 );
1533
- auto readBuffer = co_await contentStream.ReadAsync (buffer, buffer.Capacity (), InputStreamOptions::None);
1571
+ auto readBuffer{ co_await contentStream.ReadAsync (buffer, buffer.Capacity (), InputStreamOptions::None) };
1572
+ // readBuffer.
1534
1573
readContents = winrt::to_string (CryptographicBuffer::ConvertBinaryToString (BinaryStringEncoding::Utf8, readBuffer));
1535
1574
read += readBuffer.Length ();
1536
1575
totalRead += read;
0 commit comments