@@ -1120,29 +1120,47 @@ winrt::fire_and_forget RNFetchBlob::fetchBlob(
11201120
11211121 winrt::Windows::Web::Http::HttpRequestMessage requestMessage{ httpMethod, Uri{url} };
11221122 bool pathToFile{ body.rfind (prefix, 0 ) == 0 };
1123- winrt::hstring fileContent;
11241123 if (pathToFile)
11251124 {
11261125 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) };
11291133
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 };
11331135
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) };
11351147
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)
11391149 {
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 ()));
11411161 }
11421162 }
11431163
1144- requestMessage.Content (requestContent);
1145-
11461164 auto exists{ uploadProgressMap.find (taskId) };
11471165 if (exists != uploadProgressMap.end ()) {
11481166 auto progress{ uploadProgressMap[taskId] };
@@ -1280,35 +1298,55 @@ winrt::fire_and_forget RNFetchBlob::fetchBlobForm(
12801298
12811299 auto data{ items[" data" ].AsString () };
12821300 bool pathToFile{ data.rfind (prefix, 0 ) == 0 };
1283- winrt::hstring fileContent;
12841301 if (pathToFile)
12851302 {
12861303 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+ }
12981317
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+ }
13071330 }
13081331 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+ }
13111336
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+ }
13121350 }
13131351 requestMessage.Content (requestContent);
13141352
@@ -1530,7 +1568,8 @@ try
15301568 for (;;)
15311569 {
15321570 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.
15341573 readContents = winrt::to_string (CryptographicBuffer::ConvertBinaryToString (BinaryStringEncoding::Utf8, readBuffer));
15351574 read += readBuffer.Length ();
15361575 totalRead += read;
0 commit comments