Reputation: 1231
I'm trying to upload an xml file to a Azure File Share in a Storage account, but i keep getting this error:
The range specified is invalid for the current size of the resource.
I am able to download files from the same share, so the connection itself works
I have created a xml file which looks like this:
<?xml version="1.0" encoding="utf-16"?>
<WebOrderList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<WebOrder>
<OrderId>1</OrderId>
<Info>info about order</Info>
<Description>testing</Description>
<WebItemListWebItem>
<ItemNo>321</ItemNo>
<ParentID>0</ParentID>
</WebItemListWebItem>
</WebOrder>
</WebOrderList>
The method uploading the content looks like this:
public async Task UploadFile(string content, string path, string name)
{
var fileClient = new ShareFileClient(_connectionString, _shareName, path);
byte[] byteArray = Encoding.UTF8.GetBytes(content);
using (var stream = new MemoryStream(byteArray))
{
await fileClient.UploadAsync(stream);
}
}
where content is the xml string above, path is /orders/fromwebshop which is an existing folder on the file share, and name is order-1.xml.
Error:
Windows-Azure-File/1.0,Microsoft-HTTPAPI/2.0
x-ms-error-code: InvalidRange
Date: Thu, 24 Sep 2020 06:39:39 GMT
Content-Length: 249
Content-Type: application/xml
UPDATE:
I found out that this works:
If you want to copy:
//ExampleData
//Content = string as Xml.
//Path = "$"/orders/fromwebshop""
//Name = "$"order-{order.WebOrder[0].OrderId}.xml"
public async Task UploadFile(string content, string path, string name)
{
var newMove = new ShareFileClient(_connectionString, _shareName, $"{path}/{name}");
byte[] byteArray = Encoding.UTF8.GetBytes(content);
await newMove.CreateAsync(byteArray.Length);
using (var stream = new MemoryStream(byteArray))
{
await newMove.UploadAsync(stream);
}
}
Upvotes: 5
Views: 2142
Reputation: 1231
UPDATE:
I found out that this works:
If you want to copy the code:
//ExampleData
//Content = string as Xml.
//Path = "$"/orders/fromwebshop""
//Name = "$"order-{order.WebOrder[0].OrderId}.xml"
public async Task UploadFile(string content, string path, string name)
{
var newMove = new ShareFileClient(_connectionString, _shareName, $"{path}/{name}");
byte[] byteArray = Encoding.UTF8.GetBytes(content);
await newMove.CreateAsync(byteArray.Length);
using (var stream = new MemoryStream(byteArray))
{
await newMove.UploadAsync(stream);
}
}
Upvotes: 4