Solved

HttpContext Wierd Values

Posted on 2013-01-02
6
338 Views
Last Modified: 2013-01-05
I writing out the content to HTTP response and I’m adding the header type as CSV. The exports works fine in local, QA and staging but in production I see a hex value at the beginning and 0 at the end. Since I’m not able to reproduce the same in local or QA I’m having a hard time debugging this. Have any of you seen this before ?

This is how I’m writing to response stream. The UI makes an http post to the ExportCSV.

                              [HttpPost]
             public void ExportCSV()
             {
HttpContextBase context = HttpContext;
                           context.Response.Clear();
                           context.Response.ContentType = "text/csv";
                           context.Response.AddHeader("Content-Disposition", "attachment; filename=" + "xxx" + Guid.NewGuid().ToString() + ".csv");
            context.Response.Write("XXXXX”);
                          context.Response.End()
}


This is what I’m seeing in the export

22d (Hex value proportional to content length)

Data

0
0
Comment
Question by:optimus376
  • 3
  • 2
6 Comments
 
LVL 11

Assisted Solution

by:MajorBigDeal
MajorBigDeal earned 250 total points
ID: 38738447
Just a thought, but is the string coming from the same place in production vs the other environments, are the library versions the same, and is the character set UTF-8 in both cases?  

I remember back in my VB6 days I was using a different character set (I'm not sure if it was UCS-2 or UTF-16) but each character required exactly 2 bytes (unlike UTF-8) and my strings had a length parameter in the front, and a zero termination at the end which seems kind of similar to what you are describing.

So if the string is coming from an older version of software than in production vs the other environments, that might account for it.
0
 

Author Comment

by:optimus376
ID: 38738459
Thanks MajorBigDeal. The data is actually coming from UI. The server side just formats the data and returns the reponse as csv. Do you think forcing

Response.ContentType = "text/csv; charset=utf-8"; will help ?
0
 
LVL 23

Accepted Solution

by:
Roopesh Reddy earned 250 total points
ID: 38739409
Hi,

Is this an MVC application? If yes, then try FileResult - http://msdn.microsoft.com/en-us/library/system.web.mvc.fileresult(v=vs.108).aspx

Moreover, are you providing the full path of the file like this -
filename=" + "xxx" + Guid.NewGuid().ToString() + ".csv" in  context.Response.Write("XXXXX”);

Try this, as suggested in this article - http://www.c-sharpcorner.com/UploadFile/d3e4b1/saving-reading-uploading-downloading-mechanism-of-csv-fil/

Hope it helps u...
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 38739517
It might help, I'm not sure.  One thing that would be good to know is if the difference in in the actual string coming from the UI, or if the difference is being introduced when the string is processed for output.  That would be a big clue as to what is going on.
0
 

Author Comment

by:optimus376
ID: 38745750
I changed the function return type as FileResult and returned a File(stream, filename) and it magically fixed things. THanks :)
0
 

Author Comment

by:optimus376
ID: 38747011
I've requested that this question be closed as follows:

Accepted answer: 0 points for optimus376's comment #a38745750

for the following reason:

Found the solution myself
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now