Download a File From a External sever through Asp.net & C# - c#

First,I uploaded a file in a external server and got a url from that server.Now i want to download that uploaded file from the external server through that url which i got from that server through asp.net and c#.
I wrote a c# code to download that file, but during that process i got an exception
"Exception of type 'System.OutOfMemoryException' was thrown".
The following is the c# code which i wrote to download:
double dlsize=0;
string Url = "http://www.sample.com/download.zip"; \\File Size: 75MB
int lastindex = Url.LastIndexOf("/");
string TempUrlName = Url.Substring(lastindex + 1, Url.Length - (lastindex + 1));
WebRequest oWebRequest = WebRequest.Create(Url);
oWebRequest.Timeout = -1;
WebResponse oWebResponse = oWebRequest.GetResponse();
if (oWebResponse.ContentType != "text/html; charset=UTF-8")
{
string myFile = oWebResponse.Headers.Get("Content-Length");
int TempmyFile = Convert.ToInt32(myFile);
double bytes_total = Convert.ToDouble(TempmyFile);
dlSize = Convert.ToDouble(bytes_total / (1024 * 1024));
System.IO.MemoryStream oMemoryStream = new MemoryStream();
byte[] buffer = new byte[4096];
using (System.IO.Stream oStream = oWebResponse.GetResponseStream())
{
int Count = 0;
do
{
Count = oStream.Read(buffer, 0, buffer.Length);
oMemoryStream.Write(buffer, 0, Count);
} while (Count != 0);
}
System.IO.FileStream oFileStream = new System.IO.FileStream("C:\Documents and Settings\arun\Desktop\New Folder\download.zip", System.IO.FileMode.Create);
oMemoryStream.WriteTo(oFileStream);
oFileStream.Flush();
oFileStream.Close();
oMemoryStream.Flush();
oMemoryStream.Close();
oWebResponse.Close();
}

It would be easier to do it like this:
WebClient webClient = new WebClient();
webClient.DownloadFile(remoteFileUrl, localFileName);

Related

Intercepting requested file path & Open file from different source in new tab

I have 4.7.2 application where on anchor tag we have file link like 'www.abc.com/a.txt'. If I click it file is opening in new tab.
But now expected result is - When user hit that link, I want to intercept this link & change it to 'www.xyz.com/a.txt' & return response in new tab. Basically open file in new tab (no download).
Currently I'm able to download file using below code but I want to open in new tab.
Stream stream = null;
int bytesToRead = 10000;
byte[] buffer = new Byte[bytesToRead];
try
{
string apiGatewayFilePath = "www.xyz.com/a.txt";
HttpWebRequest fileReq = (HttpWebRequest)WebRequest.Create(apiGatewayFilePath);
string fileName = Path.GetFileName(apiGatewayFilePath);
HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();
if (fileReq.ContentLength > 0)
fileResp.ContentLength = fileReq.ContentLength;
stream = fileResp.GetResponseStream();
var resp = HttpContext.Current.Response;
resp.ContentType = MediaTypeNames.Application.Octet;
resp.AddHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());
int length;
do
{
if (resp.IsClientConnected)
{
length = stream.Read(buffer, 0, bytesToRead);
resp.OutputStream.Write(buffer, 0, length);
resp.Flush();
buffer = new Byte[bytesToRead];
}
else
{
length = -1;
}
} while (length > 0);
}
finally
{
if (stream != null)
{
stream.Close();
}
}

OneDrive REST API - Requested Range Error While Uploading Chunk

I've been trying to upload files to my OneDrive via HTTP Requests following this document (https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession?view=odsp-graph-online) without success. I have the following steps rounded up (Authentication, folder creation for the file, create an upload session) but when I try the last step, byte upload to the created session, I get this error in the second PUT request:
Requested Range Not Satisfiable {"error":{"code":"invalidRange","message":"Optimistic concurrency failure during fragmented upload"}}
This is my code:
//Get File Data
byte[] FileByteArray = File.ReadAllBytes(FilePath);
//Create Upload Session
OutlookEndpoint = $"{AppSettings.DriveSettings.OneDriveSettings.Endpoint}/me/drive/items/{FolderId}:/{Name}:/createuploadsession";
OutlookResponseMessage = await OutlookClient.PostAsync(OutlookEndpoint, new StringContent("{}", Encoding.UTF8, "application/json"));
OutlookResponseContent = await OutlookResponseMessage.Content.ReadAsStringAsync();
if (OutlookResponseMessage.IsSuccessStatusCode)
{
OutlookUpload OutlookUpload = JsonConvert.DeserializeObject<OutlookUpload>(OutlookResponseContent);
//Check the Created URL
if (!string.IsNullOrEmpty(OutlookUpload.UploadUrl))
{
//Chunk Calculation
int TotalSize = FileByteArray.Length;
int AcumulativeSize = 0;
int ChunkSize = 327680;
int ChunkBuffer = ChunkSize;
int ChunkNumber = TotalSize / ChunkSize;
int ChunkLeftover = TotalSize - ChunkSize * ChunkNumber;
int ChunkCounter = 0;
while (true)
{
if (ChunkNumber == ChunkCounter)
{
ChunkSize = ChunkLeftover;
}
byte[] ChunkData = FileByteArray.Skip(ChunkBuffer * ChunkCounter).Take(ChunkSize).ToArray();
AcumulativeSize += ChunkData.Length;
//PUT Upload of Chunk
string UploadEndpoint = OutlookUpload.UploadUrl;
string BytesHeader = $"bytes {AcumulativeSize - ChunkSize}-{AcumulativeSize - 1}/{TotalSize}";
OutlookClient.DefaultRequestHeaders.Clear();
OutlookClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
OutlookClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Length", ChunkSize.ToString());
OutlookClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Range", BytesHeader);
OutlookResponseMessage = await OutlookClient.PutAsync(UploadEndpoint, new ByteArrayContent(ChunkData));
OutlookResponseContent = await OutlookResponseMessage.Content.ReadAsStringAsync();
if (OutlookResponseMessage.IsSuccessStatusCode)
{
Console.WriteLine("SUCCESS");
}
else
{
Console.WriteLine(OutlookResponseMessage.ReasonPhrase);
}
if (ChunkNumber == ChunkCounter)
{
break;
}
ChunkCounter++;
}
}
}
Perhaps I'm missing something. I only get a SUCCESS message in the first PUT request, the others always give me the error described above. Here's an image of the error with the headers I send. Image
I'd appreciate any help, thanks for reading this far.
EDIT:
Got it working after modifying the the header configuration for the request and changing the way chunks are created.
//Get File Data
byte[] FileByteArray = File.ReadAllBytes(FilePath);
//Create Upload Session
OutlookEndpoint = $"{AppSettings.DriveSettings.OneDriveSettings.Endpoint}/me/drive/items/{FolderId}:/{Name}:/createuploadsession";
OutlookResponseMessage = await OutlookClient.PostAsync(OutlookEndpoint, new StringContent("{}", Encoding.UTF8, "application/json"));
OutlookResponseContent = await OutlookResponseMessage.Content.ReadAsStringAsync();
if (OutlookResponseMessage.IsSuccessStatusCode)
{
OutlookUpload OutlookUpload = JsonConvert.DeserializeObject<OutlookUpload>(OutlookResponseContent);
//Check the Created URL
if (!string.IsNullOrEmpty(OutlookUpload.UploadUrl))
{
using MemoryStream FileStream = new MemoryStream(FileByteArray);
//Chunk Calculation
int ChunkSize = 320 * 1024;
int ChunkRemaining = 0;
byte[] ByteBuffer = new byte[ChunkSize];
int BytesRead = 0;
while ((BytesRead = FileStream.Read(ByteBuffer, 0, ByteBuffer.Length)) > 0)
{
if (BytesRead < ChunkSize)
{
byte[] LastBuffer = new byte[BytesRead];
Buffer.BlockCopy(ByteBuffer, 0, LastBuffer, 0, BytesRead);
ByteBuffer = new byte[BytesRead];
ByteBuffer = LastBuffer;
}
try
{
OutlookClient.DefaultRequestHeaders.Clear();
string UploadEndpoint = OutlookUpload.UploadUrl;
string BytesHeader = $"bytes {ChunkRemaining}-{ChunkRemaining + ByteBuffer.Length - 1}/{FileByteArray.Length}";
HttpRequestMessage MicrosoftResponseMessage = new HttpRequestMessage()
{
Content = new ByteArrayContent(ByteBuffer),
RequestUri = new Uri(UploadEndpoint),
Method = HttpMethod.Put,
};
MicrosoftResponseMessage.Content.Headers.Add("Content-Length", ByteBuffer.Length.ToString());
MicrosoftResponseMessage.Content.Headers.Add("Content-Range", BytesHeader);
OutlookResponseMessage = await OutlookClient.SendAsync(MicrosoftResponseMessage);
OutlookResponseContent = await OutlookResponseMessage.Content.ReadAsStringAsync();
if (OutlookResponseMessage.IsSuccessStatusCode)
{
Console.WriteLine("SUCCESS");
ChunkRemaining += ByteBuffer.Length;
if (ChunkRemaining == FileByteArray.Length)
{
Console.WriteLine("COMPLETED");
}
}
else
{
Console.WriteLine(OutlookResponseMessage.ReasonPhrase);
}
}
catch (Exception Exception)
{
Console.WriteLine(Exception.Message);
break;
}
}
}
}
Please note that on failures when the client sent a fragment the server had already received, the server will respond with HTTP 416 Requested Range Not Satisfiable. You can request upload status to get a more detailed list of missing ranges. Apparently the content-range and content-length were the problem. You changed the header configuration from the HttpClient to a HttpRequestMessage and it worked perfectly now.

Download a file with .net compact and C#

I am re-developing an app for a scanner used for stocktakes to allow it to work while offline. In order to do so, I need to be able to download a file from a laptop which is acting as a server. I got to a point at which it works, but only downloads that are of size 9.53mb max. How can I tweak the code to allow for larger files. I would need to allow for a maximum size of around 30mb.
Here is my code:
try
{
string full_url = App.prouductUrl + App.stStocktakeId + ".db";
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(full_url);
httpRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
System.IO.Stream dataStream = httpResponse.GetResponseStream();
// Dim str As Stream = cdsMobileLibrary2.http_download.getfile(filename)
//50 meg
byte[] inBuf = new byte[10000001];
int bytesToRead = Convert.ToInt32(inBuf.Length);
int bytesRead = 0;
while (bytesToRead > 0)
{
int n = dataStream.Read(inBuf, bytesRead, bytesToRead);
if (n == 0)
{
break; // TODO: might not be correct. Was : Exit While
}
bytesRead += n;
bytesToRead -= n;
}
FileStream fstr = new FileStream(#"\productdb\" + App.stStocktakeId + ".db", FileMode.OpenOrCreate, FileAccess.Write);
fstr.Write(inBuf, 0, bytesRead);
dataStream.Close();
fstr.Close();
string size = loginRes.getFromJSON("size");
FileInfo fi = new FileInfo(#"\productdb\" + App.stStocktakeId + ".db");
MessageBox.Show("File Size is:" + fi.Length + "Compared to:" + size);
}
catch { }

PhoneGap Build API Error when Posting .ZIP file Greater than 127kb

I wrote the C# application below to update an existing application via the PhoneGap Build API. I noticed it works when my .ZIP file is 127kb or less. As soon as it hits 128kb, I get a 500 HTTP response. Sorry, the API does not return any details on the error, only the 500 response code. Any help with this issue will be greatly appreciated. Please note the placeholders for authentication token, appId and .zip file location. Thanks.
using System;
using System.IO;
using System.Net;
namespace PhoneGapBuildQuestion
{
class Program
{
static void Main(string[] args)
{
string token = "<add your token here>";
string appId = "<add your appId here>";
string zipFile = "<add full path to the application .zip file here>";
var info = new FileInfo(zipFile);
var request = (HttpWebRequest)WebRequest.Create(string.Format("https://build.phonegap.com/api/v1/apps/{0}?auth_token={1}", appId, token));
request.ContentType = "application/zip";
request.Headers["Content-disposition"] = string.Format("attachment; filename=\"{0}\"", info.Name);
request.Method = "PUT";
var reqStream = request.GetRequestStream();
var file = new FileStream(zipFile, FileMode.Open);
var bytes = new byte[32768];
int len = 0;
while((len = file.Read(bytes, 0, bytes.Length)) > 0)
reqStream.Write(bytes, 0, len);
reqStream.Close();
var response = new StreamReader(request.GetResponse().GetResponseStream());
string responseText = response.ReadToEnd();
Console.WriteLine(responseText);
Console.ReadLine();
}
}
}
I figured it out. I used fiddler to capture the request from my app and cURL, compared the two and adjusted accordingly. Here is the code I ended up with:
using System;
using System.IO;
using System.Net;
namespace PhoneGapBuildQuestion
{
class Program
{
static void Main(string[] args)
{
string appId = "[your appId here]";
string fileName = "[absolute path to .zip file here]";
string token = "[authentication token here]";
string boundry = "----------------------------7b053ae48e94";
var encoding = new System.Text.ASCIIEncoding();
var fileInfo = new FileInfo(fileName);
var ms = new MemoryStream();
long totalBytes = 0;
string txt = string.Format("--{0}{2}Content-Disposition: form-data; name=\"file\"; filename=\"{1}\"{2}Content-Type: application/octet-stream{2}{2}", boundry, fileInfo.Name, Environment.NewLine);
int bytesRead = 0;
var buffer = new byte[32768];
bytesRead = encoding.GetBytes(txt, 0, txt.Length, buffer, 0);
totalBytes += bytesRead;
ms.Write(buffer, 0, bytesRead);
// read/write file contents to the stream
var fs = new FileStream(fileName, FileMode.Open);
while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, bytesRead);
totalBytes += bytesRead;
}
txt = Environment.NewLine + "--" + boundry + "--" + Environment.NewLine;
bytesRead = encoding.GetBytes(txt, 0, txt.Length, buffer, 0);
totalBytes += bytesRead;
ms.Write(buffer, 0, bytesRead);
ms.Position = 0;
var request = (HttpWebRequest)WebRequest.Create(string.Format("https://build.phonegap.com/api/v1/apps/{0}?auth_token={1}", appId, token));
request.ContentLength = totalBytes;
request.Method = "PUT";
request.ContentType = "multipart/form-data; boundary=" + boundry;
var requestStream = request.GetRequestStream();
while ((bytesRead = ms.Read(buffer, 0, buffer.Length)) > 0)
requestStream.Write(buffer, 0, bytesRead);
requestStream.Close();
Console.WriteLine(new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd());
Console.ReadLine();
}
}
}

C# remote stream for file transfer does not read

i have written a little "Update Programm" to keep an .exe up to date for the rest of my dev team. It used to work fine, but suddenly it stopped working.
I already noticed the problem: my remote stream does not start to read.
Uri patch = new Uri("http://********/*********/" + GetVersion().ToString() + ".exe");
Int64 patchsize = PatchSize(patch);
var CurrentPath = String.Format("{0}\\", Environment.CurrentDirectory);
Int64 IntSizeTotal = 0;
Int64 IntRunning = 0;
string strNextPatch = (version + ".exe");
using (System.Net.WebClient client = new System.Net.WebClient())
{
using (System.IO.Stream streamRemote = client.OpenRead(patch))
{
using (System.IO.Stream streamLocal = new FileStream(CurrentPath + strNextPatch, FileMode.Create, FileAccess.Write, FileShare.None))
{
int intByteSize = 0;
byte[] byteBuffer = new Byte[IntSizeTotal];
while ((intByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
{
streamLocal.Write(byteBuffer, 0, intByteSize);
IntRunning += intByteSize;
double dIndex = (double)(IntRunning);
double dTotal = (double)byteBuffer.Length;
double dProgressPercentage = (dIndex / dTotal);
int intProgressPercentage = (int)(dProgressPercentage * 100);
worker.ReportProgress(intProgressPercentage);
}
streamLocal.Close();
}
streamRemote.Close();
GetVersion() only returns the current version number of the current server version of the .exe.
The problem lies here:
while ((intByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
My streamRemote just does not return any bytes so this while clause is not filled.
Any advice for me?
I believe the problem is on the server. I'd run some checks:
Has anything changed on the configuration of the web server that stops you from downloading executables?
Are you connecting through a proxy?
Can you manually get to the same URL (under the same user credentials of your application)?

Categories