So im working on a .raw file uploader made in C# winform, and its working fine but any files a little bit above 2GB don't upload to the server.
Im using the RestSharp library, heres the upload function
private void uploadfile(string filelocation)
{
output.AppendText(Environment.NewLine + DateTime.Now + $" Start Uploading {filelocation}");
string newfilelocation;
if (nocopy.Checked) {
newfilelocation = filelocation;
}
else
{
newfilelocation = Path.GetDirectoryName(filelocation) + #"\temp\" + Path.GetFileName(filelocation);
//check if a temp patch exits, create one if not.
string temppath = Path.GetDirectoryName(filelocation) + #"\temp\";
bool exists = System.IO.Directory.Exists(temppath);
if (!exists)
System.IO.Directory.CreateDirectory(temppath);
if (File.Exists(newfilelocation))
{
File.Delete(newfilelocation);
}
try //for somereason, copy file fails sometimes...
{
File.Copy(filelocation, newfilelocation, true);
}
catch
{
output.AppendText(Environment.NewLine + DateTime.Now + $" {newfilelocation} uploading failed once will try again");
System.Threading.Thread.Sleep(30000);
try //2nd try
{
File.Copy(filelocation, newfilelocation, true);
}
catch
{
output.AppendText(Environment.NewLine + DateTime.Now + $" {newfilelocation} uploading failed second time");
return;
}
// Todo: Additional recovery here,
// like telling the calling code to re-open the file selection dialog
}
}
var client = new RestClient(txtserver.Text);
client.Timeout = 30 * 60 * 1000;// 1000 ms = 1s, 30 min = 30*60*1000
client.Authenticator = new HttpBasicAuthenticator(txtusername.Text, txtpassword.Text);
if (!File.Exists(newfilelocation))
{
MessageBox.Show("Please check file location");
return;
}
var request = new RestRequest();
request.Method = Method.POST;
request.AddHeader("Accept", "application/json");
request.Parameters.Clear();
request.AddHeader("Content-Type", "multipart/form-data");
if(String.IsNullOrEmpty(txtsamplename.Text))
{
request.AddParameter("run_name", Path.GetFileNameWithoutExtension(newfilelocation));
}
else
{
request.AddParameter("run_name", txtsamplename.Text);
}
request.AddParameter("project_name", txtprojectname.Text);
request.AddParameter("run_desc", txtdescription.Text);
request.AddParameter("qc_tool", qctool.SelectedIndex);
request.AddParameter("temp_data", TempData.Checked);
request.AddFile("rawfile", newfilelocation);
request.ReadWriteTimeout = 2147483647;
request.Timeout = 2147483647;
var response = client.Execute(request);
output.AppendText(Environment.NewLine + response.Content);
output.AppendText(Environment.NewLine + DateTime.Now + $" {newfilelocation} uploaded");
if (!nocopy.Checked)
{
File.Delete(newfilelocation);
}
}
Wondering what solutions there could be so i can upload larger files. The server isnt the issue, since the files can be uploaded to the server using a browser extension. Its on the winform app side.
Related
I am creating a API consumption tool where I have a issue in which It is giving following error when I try to call API. Please help me with this. I am trying to get CSV file and converted to TXT format with this API.
System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[System.String,StarRezToolApp.Program+d__2]
public static void GetReportInformation(string file_path_1, string Filename)
{
Utility.Utility.Log("TestFIle Reached");
var report_data = HTTP_GET();
Console.WriteLine(report_data.ToString());
var sb_csv = new StringBuilder();
try
{
if (File.Exists(file_path_1 + Filename))
{
using (StreamWriter apiresponse = File.AppendText(file_path_1 + Filename))
{
apiresponse.Write(report_data.ToString());
apiresponse.WriteLine();
}
}
else
{
using (StreamWriter apiresponse = new StreamWriter(file_path_1 + Filename))
{
apiresponse.Write(report_data.ToString());
apiresponse.WriteLine();
}
}
Utility.Utility.Log("File Created Successfully.");
}
catch (Exception ex)
{
Utility.Utility.Log("Error: Could Not Convert. Original error: " + ex.Message);
}
}
I have been calling the following method for other Information
private static async Task<string> HTTP_GET()
{
var TARGETURL = Properties.Resources.URL + Properties.Resources.Report_Name;
Console.WriteLine("GET: + " + TARGETURL);
Utility.Utility.Log("GET: + " + TARGETURL);
NetworkCredential credentials = new NetworkCredential(Properties.Resources.Username, Properties.Resources.Tocken.ToString());
HttpClientHandler handler = new HttpClientHandler
{
Credentials = credentials
};
// ... Use HttpClient with handlers which has credentials
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = await client.GetAsync(TARGETURL);
HttpContent content = response.Content;
// ... Check Status Code
Utility.Utility.Log("Response StatusCode: " + (int)response.StatusCode);
Console.WriteLine("Response StatusCode: " + (int)response.StatusCode);
// ... Read the string.
string result = await content.ReadAsStringAsync();
// ... Display the result.
if (result != null && result.Length >= 50)
{
Utility.Utility.Log("Response message: Successful");
return result.ToString();
}
else
{
Utility.Utility.Log("Response message: " + response.Content);
return null;
}
}
Thank you Mr. #RuardvanElburg. I got the solution by your help.
My controller method GetReportInformationAsync needs to await for response to get out.
So using Wit.ai I'm trying to use speech to text. I am using the Wit3D example from Github: https://github.com/afauch/wit3d/blob/master/Assets/UserScripts/Wit3D.cs
Recording of sound and saving to the .wav file works just fine. Sending the request to the server does not.
The .wav file is valid as I get a response when manually making a request through Postman.
The request code looks like this:
string GetJSONText(string file)
{
// get the file w/ FileStream
FileStream filestream = new FileStream(file, FileMode.Open, FileAccess.Read);
BinaryReader filereader = new BinaryReader(filestream);
byte[] BA_AudioFile = filereader.ReadBytes((Int32)filestream.Length);
filestream.Close();
filereader.Close();
//var bytes = File.ReadAllBytes(Path.Combine(Application.dataPath, "sample.wav"));
// create an HttpWebRequest
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.wit.ai/speech?v=20160901");
request.Method = "POST";
request.Headers["Authorization"] = "Bearer 3XFWDOBVS65V5A2VZWZFBB2PHOKDWGOH";
request.ContentType = "audio/wav";
//request.Timeout = 10000;
request.GetRequestStream().Write(BA_AudioFile, 0, BA_AudioFile.Length);
// Process the wit.ai response
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
print("Http went through ok");
StreamReader response_stream = new StreamReader(response.GetResponseStream());
return response_stream.ReadToEnd();
}
else
{
return "Error: " + response.StatusCode.ToString();
return "HTTP ERROR";
}
}
catch (Exception ex)
{
return "Error: " + ex.Message;
return "HTTP ERROR";
}
}
With or without putting a Timeout on the request I get the following Error message: "Error: The request timed-out"
Removing the line:
request.GetRequestStream().Write(BA_AudioFile, 0, BA_AudioFile.Length)
Will get me a response:
Error: Error getting response stream (Write: The authentication or decryption has failed.)
Which makes sense, because there is nothing to decrypt.
My firewall doesn't seem to be the problem. Any ideas of why there is a time-out? Using different methods of getting a byte[] didn't fix it either.
EDIT:
Putting the code in a normal Console application does work. So this seems to be a Unity issue.
Add this to top of your script:
using System.Collections.Generic;
Use this code.
public void SendRequest(string wavPath)
{
if(!File.Exists(wavPath))
{
Debug.Log("Invalid wav path.");
return;
}
StartCoroutine(SendRequestToWitAi(wavPath));
}
public IEnumerator SendRequestToWitAi(string wavPath)
{
string API_KEY = "3XFWDOBVS65V5A2VZWZFBB2PHOKDWGOH";
string url = "https://api.wit.ai/speech?v=20160526";
byte[] postData = File.ReadAllBytes(wavPath);
Dictionary<string, string> headers = new Dictionary<string, string>();
headers["Content-Type"] = "audio/wav";
headers["Authorization"] = "Bearer " + API_KEY;
float timeSent = Time.time;
WWW www = new WWW(url, postData, headers);
yield return www;
while (!www.isDone)
{
yield return null;
}
float duration = Time.time - timeSent;
if (www.error != null && www.error.Length > 0)
{
Debug.Log("Error: " + www.error + " (" + duration + " secs)");
yield break;
}
Debug.Log("Success (" + duration + " secs)");
Debug.Log("Result: " + www.text);
}
Use a JSON parser to parse www.text value. The "_text" field contains the result text.
I'm using HttpClient to post a binary file to the server which is hosted by Azure. The request is taking to0 long to get across for files that aren't that big in size. Some times the server receives the request when the client has already cancelled the task due to time out. I'm uploading the data asynchronously using the below code:
public async Task<HttpResponseMessage> UploadFile(byte[] file)
{
videoThumbName = Guid.NewGuid().ToString();
var progress = new System.Net.Http.Handlers.ProgressMessageHandler();
progress.HttpSendProgress += progress_HttpSendProgress;
using (var client = HttpClientFactory.Create(progress))
{
client.BaseAddress = new Uri(GlobalVariables.host);
// Set the Accept header for BSON.
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/bson"));
var request = new uploadFileModel { data = file, dateCreated = DateTime.Now, fileName = fileName, username = loggedUser, VideoThumbName = videoThumbName};
// POST using the BSON formatter.
MediaTypeFormatter bsonFormatter = new BsonMediaTypeFormatter();
var m = client.MaxResponseContentBufferSize;
var result = await client.PostAsync("api/media/upload", request, bsonFormatter);
return result.EnsureSuccessStatusCode();
}
}
The server side code looks like this (I have left some code for brevity):
[HttpPost]
[Route("upload")]
public async Task<HttpResponseMessage> Upload(uploadFileModel model)
{
var result = new HttpResponseMessage(HttpStatusCode.OK);
if (ModelState.IsValid)
{
string thumbname = model.VideoThumbName;
string tempPath = HttpContext.Current.Server.MapPath("~/video");
string sourceFilePath = Path.Combine(HttpContext.Current.Server.MapPath("~/video"), model.fileName);
string pathToThumbs = Path.Combine(HttpContext.Current.Server.MapPath("~/contents/member/" + model.username + "/thumbs"), thumbname + ".jpg");
string finalPath = Path.Combine(HttpContext.Current.Server.MapPath("~/contents/member/" + model.username + "/flv"), model.fileName);
string thumbPath = HttpContext.Current.Server.MapPath("~/contents/member/" + model.username + "/thumbs");
string vidDuration = "";
string videoDurationSec = "";
int maxWidth = 380;
int maxHeight = 360;
try
{
File.WriteAllBytes(sourceFilePath, model.data);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
var ffMpegThumb = new NReco.VideoConverter.FFMpegConverter();
ffMpegThumb.GetVideoThumbnail(sourceFilePath, pathToThumbs);
var ffmpegVid = new NReco.VideoConverter.FFMpegConverter();
ffmpegVid.ConvertMedia(sourceFilePath, Format.mp4, finalPath, Format.mp4, new ConvertSettings() { VideoCodec = "h264" });
return result;
}
else
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
}
}
Is it possible that it's due to my internet connection request gets across late? I'm sending files that aren't even that big in size, they are like 1-2 mbs in size. I wonder how long it will take for bigger files.
Is there anyway I can improve this process?
These are the following are my requirements.
There is URL from SSRS Report which renders the report in EXCEL file
We have setup for MVC application as web app and the webapi as App server.
Since the ssrs inside the firewall the app server will connect only through Appserver. In Appserver, The report has been rendered as follows
public HttpResponseMessage GenerateLegacyReport()
{
try
{
string endTypeString = "&rs:Format=Excel";
url = url + businessSegmentFormat(businessSegment) + regionStringFormat(region) +
facilitySiteStringFormat(selectedSite) + fromDateFormat(FromDate) +
toDateFormat(ToDate) + salesPersonFormat(userName) + SortstringFormat(sortOrder) +
buildSortOrderUrl(sortByList) + groupByFormat(reportName, groupBy) + endTypeString;
//AlertMailer setalert = new AlertMailer();
//setalert.NOtifyError("Report", null, url);
if (!string.IsNullOrEmpty(url))
{
string _sessionPipelineReport = "PipelineReport" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
byte[] excelBytes = null;
using (WebClient webclient = new WebClient())
{
webclient.Credentials = CredentialCache.DefaultCredentials;
//Sample URL replaced with Actual SSRS URL
url = "https://online.utpb.edu/webapps/dur-browserCheck-bb_bb60/samples/sample.xlsx?CRMReports/CustomerProfileSummary&rs:Command=Render&BusinessSegment=%5BBusiness%20Segment%5D.%5BBUSINESS%20SEGMENT%20CODE%5D.%26%5BES%5D&Region=%5BRegion%5D.%5BREGION%20NAME%5D.%5BAll%5D&Facility=%5BFacility%5D.%5BFACILITY%20NAME%5D.%5BAll%5D&FromDate=%5BEARNED%20DATE%5D.%5BDATE%5D.%26%5B2015-01-26T00:00:00%5D&ToDate=%5BEARNED%20DATE%5D.%5BDATE%5D.%26%5B2016-01-26T00:00:00%5D&User=%5BSalesperson%5D.%5BUSERNAME%5D.%26%5BAPeterson%5D&SortOrder=Desc&SortBy=Revenue&rs:Format=Excel";
excelBytes = webclient.DownloadData(url);
}
//var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(excelBytes) };
//result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
//result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline")
var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(excelBytes) };
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline")
{
FileName = _sessionPipelineReport
};
return result;
}
return Request.CreateResponse(HttpStatusCode.Gone);
}
catch (Exception ex)
{
throw new Exception("Internal Server Error. Please try again...");
}
}
And we have start reading this from Web app and render it as
Response.Redirect("https://view.officeapps.live.com/op/embed.aspx?src=" +fileURL , false);
We are able to download the report but excel file nor rendered it int he viewer.
Please suggest us the solutions are any other better online viewer (supports MVC and HTML 5)
If we get the information with any sample solution would be more appreciated :)
I have a client-server type application with the server running HttpListener and the client uploading data to the server using WebClient.UploadData. The code works quite well (whith large data buffers 60K and up) except one installation where UploadData times out when data buffer size is bigger that 16384. Here is my code on the client:
internal bool UploadData(byte[] buffer, String file, String folder)
{
try
{
String uri = "http://" + GlobalData.ServerIP + ":" + GlobalData.ServerHttpPort + "/upload:";
NameValueCollection headers = new NameValueCollection();
headers.Set("Content-Type", "application/octet-stream");
headers.Set("Y-Folder", folder);
headers.Set("Y-File", file);
using (WebClient wc = new WebClient())
{
wc.Credentials = new NetworkCredential(WebUserName, WebPassword);
wc.Headers.Add(headers);
wc.UploadData(new Uri(uri), buffer);
return true;
}
}
catch (Exception ex)
{
GlobalData.ODS("Exception in UploadFile " + ex.Message);
return false;
}
}
On the server
ODS(TraceDetailLevel.Level4, "Process upload ");
HttpListenerResponse response = e.RequestContext.Response;
String disp = "";
String fil = "";
String folder = "";
Stream body = e.RequestContext.Request.InputStream;
long len64 = e.RequestContext.Request.ContentLength64;
Encoding encoding = e.RequestContext.Request.ContentEncoding;
ODS(TraceDetailLevel.Level4, "Process upload " + len64 + " bytes encoding " + encoding.EncodingName);
NameValueCollection nvp = e.RequestContext.Request.Headers;
try
{
disp = nvp["Content-Disposition"];
fil = nvp["Y-File"];
folder = nvp["Y-Folder"];
}
catch { }
BinaryReader reader = new BinaryReader(body, encoding);
byte[] data = new byte[len64];
long total = 0;
while (true)
{
int dataleft = data.Length - (int)total;
int offset = (int)total;
GlobalData.ODS("Reading binary stream offset=" + offset + " read dataleft=" + dataleft);
int cnt = reader.Read(data, offset, dataleft);
if (cnt <= 0)
{
break;
}
total += cnt;
if (len64 <= total)
{
break;
}
}
ODS(TraceDetailLevel.Level4, "Process upload: Got data "+total+" should have="+len64);
if (total == len64)
{
//process data
The code above works well on all but one installation. What is wrong?
It looks like I found the source of the problem. This one installation in question that fails my code has AVG Free Antivirus installed on a computer that runs my HTTP server code. If I disable AVG on that computer my code works. Wondering if anybody runs into similar issues with AVG.