Solved

HttpContext Wierd Values

Posted on 2013-01-02
6
345 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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