【发布时间】:2018-03-08 15:58:57
【问题描述】:
我正在用 C# 开发一个 UWP 应用程序,它使用 Azure 后端从 Azure SQL 数据库同步到本地设备 SQLite 数据库。自最初实施以来,这一直运行良好。
现在我在尝试同步较大的图像时遇到了问题。小于 ~ 3MB 的图像成功地从 StorageFile 转换为 byte[] 并保存在 SQLite 数据库中并同步到 Azure DB。但是,大于 ~ 3MB 的图像存在同步问题。它们将转换为 byte[] 并正确显示,但尝试同步失败并出现以下错误:
“HTTP 请求未包含有效的实体主体。请确保请求中存在实体主体和关联的 Content-Type 标头。”
在服务器端,这表现为格式错误的语法错误。
所有图像的处理方式完全相同。 web.config 文件已更改为允许 requestLimits maxAllowedContentLength="2147483648"。 Azure 数据库中的列设置为 varbinary(MAX)。
此代码调用方法将所选文件转换为字节[]:
byte[] image = await ImageUtils.ChooseImageFile();
这里是转换方法:
public static async Task<byte[]> ChooseImageFile()
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file == null)
return null;
byte[] bytes = null;
using (var stream = await file.OpenReadAsync())
{
bytes = new byte[stream.Size];
using (var reader = new DataReader(stream))
{
await reader.LoadAsync((uint)stream.Size);
reader.ReadBytes(bytes);
}
}
return bytes;
}
然后调用方方法将 byte[] 分配给实体的 Image 属性。
在同步时,我使用以下方法:
public async Task SyncOfflineCacheAsync()
{
try
{
await InitializeAsync();
await _client.SyncContext.PushAsync();
await PullTables();
}
catch (MobileServicePushFailedException ex)
{
if (ex.PushResult != null)
{
foreach (var error in ex.PushResult.Errors)
{
await ResolveConflictAsync(error);
}
}
}
}
在 PushAsync() 上失败并出现上述错误。
以这种方式同步图像是否存在某种隐式大小限制?我注意到 byte[] 的字符串表示对于较大的图像显示为 base64,对于较小的图像显示为十六进制,但我找不到任何关于这是否与问题有关的信息。 byte[] 本身似乎是正确的。
谢谢!
【问题讨论】:
标签: c# azure uwp synchronization