我正在创建针对 Windows 8.0 的Windows应用商店应用 我在我的电脑上安装了Windows 8.1预览版,我使用VS2012创建了应用程序,并使用BackgroundUploader和createUploadFromStreamAsync方法将文件上传到服务器,并使用了以下代码 -
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);
我可以做些什么来解决我的问题?
如何将大文件上传到服务器?
答案 0 :(得分:1)
我解决了我的问题。
现在使用此流创建上传,并使用相同的边界创建流。
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);
答案 1 :(得分:0)
您是否尝试过使用WinRT流?而不是System.Net.MemoryStream
,请尝试Windows.Storage.Streams.InMemoryRandomStream
。您可以使用RandomAccessStream.CopyAsync()
填充它。