Pergunta

I am creating Windows store app targeting to Windows 8.0
I have Windows 8.1 preview installed on my PC,I created app with VS2012 and implemented file uploading to server with BackgroundUploader and createUploadFromStreamAsync method with the fallowing code -

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);

and it is working fine to me but when test on other system with Windows 8.0 installed app getting crash with AccessViolationException was unhandled. Attempted to read or write protected memory. This is often an indication that other memory is corrupt." at the InitializeComponent. error

Now I tried to implenent BackgroundTransferContentPart for multipart upload but not success with this code -

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);

What can I do to solve my problem ?

How to Upload large file to server ?

Foi útil?

Solução

I solved my problem.

  1. Create a temporary raw file in LocalFolder and write stream which we need to upload to file (stream contains header and footer)
  2. Synchronize this newly created temporary file to storage file
  3. Get input stream from storage file
  4. Now create upload with this stream and same boundary used to create stream.

    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);
    

Outras dicas

Have you tried using a WinRT stream instead?? Instead of System.Net.MemoryStream, try Windows.Storage.Streams.InMemoryRandomStream. You can use RandomAccessStream.CopyAsync() to fill it.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top