void OnGotResponse(IAsyncResult ares) { FileAsyncResult res = (FileAsyncResult)ares.AsyncState; try { WebResponse resp = res.Request.EndGetResponse(ares); string dir = Path.GetDirectoryName(res.FilePath); lock (this) { if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } } byte[] buffer = new byte [8092]; using (var s = resp.GetResponseStream()) { using (var f = File.OpenWrite(res.FilePath)) { int nr = 0; while ((nr = s.Read(buffer, 0, buffer.Length)) > 0) { f.Write(buffer, 0, nr); } } } } catch (Exception ex) { res.Error = ex; } res.SetDone(); }
public IAsyncResult BeginDownloadSupportFile(string name, AsyncCallback cb, object state) { FileAsyncResult res = new FileAsyncResult(); res.AsyncState = state; res.Callback = cb; string cachedFile = Path.Combine(CachedFilesDir, Path.GetFileName(name)); if (File.Exists(cachedFile)) { res.FilePath = cachedFile; res.CompletedSynchronously = true; res.SetDone(); return(res); } Uri u = new Uri(new Uri(Url), name); if (u.Scheme == "file") { res.FilePath = u.AbsolutePath; res.CompletedSynchronously = true; res.SetDone(); return(res); } else { HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(u); res.FilePath = cachedFile; res.Request = req; req.BeginGetResponse(OnGotResponse, res); } return(res); }
public IAsyncResult BeginDownloadSupportFile(string name, AsyncCallback cb, object state) { FileAsyncResult res = new FileAsyncResult(); res.AsyncState = state; res.Callback = cb; string cachedFile = Path.Combine(CachedFilesDir, Path.GetFileName(name)); if (File.Exists(cachedFile)) { res.FilePath = cachedFile; res.CompletedSynchronously = true; res.SetDone(); return(res); } Uri u = new Uri(new Uri(Url), name); if (u.Scheme == "file") { res.FilePath = u.AbsolutePath; res.CompletedSynchronously = true; res.SetDone(); return(res); } res.FilePath = cachedFile; WebRequestHelper.GetResponseAsync(() => (HttpWebRequest)WebRequest.Create(u)).ContinueWith(t => { try { var resp = t.Result; string dir = Path.GetDirectoryName(res.FilePath); lock (this) { if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } } byte[] buffer = new byte [8092]; using (var s = resp.GetResponseStream()) { using (var f = File.OpenWrite(res.FilePath)) { int nr = 0; while ((nr = s.Read(buffer, 0, buffer.Length)) > 0) { f.Write(buffer, 0, nr); } } } } catch (Exception ex) { res.Error = ex; } }); return(res); }
public Stream EndDownloadSupportFile(IAsyncResult ares) { FileAsyncResult res = ares as FileAsyncResult; if (res == null) { throw new InvalidOperationException("Invalid IAsyncResult instance"); } if (res.Error != null) { throw res.Error; } return(File.OpenRead(res.FilePath)); }
public IAsyncResult BeginDownloadSupportFile (string name, AsyncCallback cb, object state) { FileAsyncResult res = new FileAsyncResult (); res.AsyncState = state; res.Callback = cb; string cachedFile = Path.Combine (CachedFilesDir, Path.GetFileName (name)); if (File.Exists (cachedFile)) { res.FilePath = cachedFile; res.CompletedSynchronously = true; res.SetDone (); return res; } Uri u = new Uri (new Uri (Url), name); if (u.Scheme == "file") { res.FilePath = u.AbsolutePath; res.CompletedSynchronously = true; res.SetDone (); return res; } res.FilePath = cachedFile; WebRequestHelper.GetResponseAsync (() => (HttpWebRequest)WebRequest.Create (u)).ContinueWith (t => { try { var resp = t.Result; string dir = Path.GetDirectoryName (res.FilePath); lock (this) { if (!Directory.Exists (dir)) Directory.CreateDirectory (dir); } byte[] buffer = new byte [8092]; using (var s = resp.GetResponseStream ()) { using (var f = File.OpenWrite (res.FilePath)) { int nr = 0; while ((nr = s.Read (buffer, 0, buffer.Length)) > 0) f.Write (buffer, 0, nr); } } } catch (Exception ex) { res.Error = ex; } }); return res; }