1

我正在创建面向Windows 8.0
的 Windows 商店应用程序 我在我的 PC 上安装了 Windows 8.1 预览版,我使用 VS2012 创建了应用程序,并使用BackgroundUploadercreateUploadFromStreamAsync方法使用以下代码实现了文件上传到服务器 -

string url = //url to upload videostring boundray = string.Format("abcboundry");
String header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\n" + "Content-Type: {3}\r\n\r\n", boundray, "file", source.Name, "application/octet-stream");
string footer = string.Format("\r\n--{0}--\r\n", boundray);

Stream headerStream = GenerateStreamFromString(header);
Stream footerStream = GenerateStreamFromString(footer);
//Stream dataStream = iRandomstream.AsStream();
Stream dataStream = await source.OpenStreamForReadAsync();

MemoryStream fileDataStream = new MemoryStream();
await headerStream.CopyToAsync(fileDataStream);
await dataStream.CopyToAsync(fileDataStream);
await footerStream.CopyToAsync(fileDataStream);
fileDataStream.Position = 0;

IInputStream stream = fileDataStream.AsInputStream();

BackgroundUploader backgroundUploader = new BackgroundUploader();
backgroundUploader.SetRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundray);
backgroundUploader.Method = "POST";

UploadOperation uploadOpration = await backgroundUploader.CreateUploadFromStreamAsync(new Uri(url), stream);

await HandleUploadAsync(uploadOpration, true);

它对我来说工作正常,但是当在安装了 Windows 8.0 的其他系统上进行测试时,由于AccessViolationException 导致崩溃的应用程序未处理。尝试读取或写入受保护的内存。这通常表明其他内存已损坏。”在 InitializeComponent.错误

现在我尝试使用BackgroundTransferContentPart进行分段上传,但这段代码没有成功 -

List<BackgroundTransferContentPart> parts = new List<BackgroundTransferContentPart>();
BackgroundTransferContentPart part = new BackgroundTransferContentPart("file", source.Name);
part.SetHeader("Content-Type", "application/octet-stream");
part.SetFile(source);
parts.Add(part);

BackgroundUploader uploader = new BackgroundUploader();
uploader.Method = "POST";
UploadOperation uploadOp = await uploader.CreateUploadAsync(new Uri(url), parts, "form-data",boundray);

await HandleUploadAsync(uploadOp, true);

我能做些什么来解决我的问题?

如何将大文件上传到服务器?

4

2 回答 2

1

我解决了我的问题。

  1. 在 LocalFolder 中创建一个临时原始文件并写入我们需要上传到文件的流(流包含页眉和页脚)
  2. 将这个新创建的临时文件同步到存储文件
  3. 从存储文件中获取输入流
  4. 现在使用此流和用于创建流的相同边界创建上传。

    StorageFile VideoFileStream = await ApplicationData.Current.LocalFolder.CreateFileAsync("tempStream", CreationCollisionOption.OpenIfExists);
    var fs2 = await VideoFileStream.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
    IInputStream aaaa= fs2.GetInputStreamAt(0);
    
    BackgroundUploader backgroundUploader = new BackgroundUploader();
    backgroundUploader.SetRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundray);
    backgroundUploader.Method = "POST";
    
    UploadOperation uploadOpration = await backgroundUploader.CreateUploadFromStreamAsync(new Uri(url),aaaa);
    await HandleUploadAsync(uploadOpration, true);
    
于 2013-09-30T11:32:08.213 回答
0

您是否尝试过使用 WinRT 流?而不是System.Net.MemoryStream,尝试Windows.Storage.Streams.InMemoryRandomStream。你可以用RandomAccessStream.CopyAsync()它来填充它。

于 2013-09-30T03:23:49.000 回答