C# - Calling Response.TransmitFile() From Static Method

Feb 16, 2010

I have a number of pages which need to support exporting data to an Excel spreadsheet. I can generate the Excel files just fine, but I'm trying to work out how to abstract this behavior so it's easily reusable from all of the pages where I need it. My current idea is to use a static utility method, as follows:

[Code]....

The click handler where I'm calling SendExcelFile looks like this:

protected void lnkExport_Click(object sender, EventArgs e)
{
List<List<string>> dataList = GatherDataForSpreadsheet();
Utility.SendExcelFile(this, "fileNameForDownload.xlsx", dataList, "MyReports");
}

This code works just fine as an instance method of the calling page. As a static method, though, it doesn't work at all. When I click the button that invokes this, the browser shows the loading animations indefinitely, but never prompts for a file download.

EDIT: If I remove the call to File.Delete() at the end, the method works as expected. Does Response.TransmitFile() do the transfer asynchronously?

EDIT 2: I just needed to call Response.Flush() before I deleted the file.

View 5 Replies


Similar Messages:

Web Forms :: Calling Response.TransmitFile From Window Dialog?

Feb 11, 2010

I create a window dialog by calling window.showModalDialog;

In that dialog I want to execute a Response.TransmitFile like the code below. The code works fine from a non dialog window. However it just hangs if I call it from the modal dialog page. No error messages. I just doesn't do anything. Is there something else I need to do to get this to function in a dialog window?

string thePath = "c:\temp\New Text Document.txt";
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + "New Text Document.txt");
Response.TransmitFile(thePath);
Response.End();

View 2 Replies

AJAX :: Calling Non-static Methods From A Static Method?

Jun 30, 2010

I did some research after posting. All I found was simple examples for no-layer architectures, like connecting to a database from your aspx page, so, in a corporate environment, it is unnaceptable.

I need to call a server-side method (using ASP.NET Ajax) in a 3-layer architecture.

For example, my Default.aspx contains a method LoadProducts().

[Code]....

[Code]....

This cannot change. There is no way to convert Business and Data layers to static.

How can I call the LoadProducts() method using ASP.NET Ajax?

View 2 Replies

Web Forms :: How To Use Httpcontext.response.transmitfile Or Other Method To Download A File From A Network Drive

Feb 25, 2010

Does anyone know how to use httpcontext.response.transmitfile or any other httpcontext function to download a file from a network drive instead of local drive?

When I tried to use context.Response.TransmitFile(DownloadPath) to download files, the DownloadPath has to be local drive. If it's a network drive, the download failed.

View 3 Replies

AJAX :: Calling Static Play Method Doesn't Work?

Aug 26, 2010

I have a page with an update contentpanel, with 4 panels inside. Image buttons outside the update panel control control which panel is visible. I am trying to call an animation from a javascript function, but the animation doesn't work, everything else does.

main.aspx:

[Code]....

main.aspx.vb:

[Code]....

web.config:

[Code]....

View 10 Replies

C# - Using Response.Filter With Response.TransmitFile?

Aug 10, 2010

I'm using Response.Filter in order to implement stream compression in accordance with HTTP Request Header Accept-Encoding

Here's the important stuff:

if (AcceptEncoding.Contains("deflate") || AcceptEncoding == "*")
{
HttpApp.Response.Filter = new DeflateStream(PreviousOutputStream, CompressionMode.Compress);
HttpApp.Response.AppendHeader("Content-Encoding", "deflate");
}


By and large this works as intended. However, I'm in a situation where I'm using an ActionResult on an MVC Controller to serve up files to the user agent:

Response.Clear();
Response.Headers["Content-Type"] = contentType;
Response.Headers["Content-Length"] = contentLength;
if (Request.QueryString["dl"] == "1")
{
Response.Headers["Content-Disposition"] = "attachment; filename=" + fileInfo.Name;
}
Response.Flush();
Response.TransmitFile(fileInfo.FullName);

To be more exact, the action method returns new EmptyResult() after the Response.TransmitFile() call. This works exactly as intended without the Response.Filter modification.

In this situation, the response entity reaches the user agent garbled and unintelligible. FireFox's Poster addon shows empty entities or jumbled entities coming back.

View 2 Replies

Response.TransmitFile() Doesn't Appear To Be Working?

Sep 7, 2010

I am calling a [WebMethod] with a piece of JQuery. The aim of the following code it to sent a recently zipped file to the user's browser. When the user clicks the 'Download' button, an AJAX request is sent to the ASP.NET page with is supposed to send the zip file. Here is the code.

[WebMethod]
public static void DownloadAlbum(string folderToDownload)
{
string archiveDir = "";

some code...

HttpContext.Current.Response.ContentType = "application/zip";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + path);
HttpContext.Current.Response.TransmitFile(path);
HttpContext.Current.Response.End();
}

When this runs, nothing is sent to the browser, but no exceptions are thrown either. I'm unsure as to what is going on and why the file is not downloading. The 'path' DEFINATELY has the correct address, and when the address of the file is hard-code the same happens.

View 2 Replies

Export To Excel Using Response.TransmitFile()?

Oct 5, 2010

using asp.net 3.5 I am trying to generate a formatted excel spreadsheet and the way i am doing it is:1.Format the excel sheet with data 2.Save it as an xml spreadhseet 3.View it in notepad/textpad and remove the "data" from the xml and have a content holder
such as "DYNAMICDATA" 4.Build dynamic xml for data and replace the "DYNAMICDATA" place holder with this fresh data.5.have all this in a stringbuilderI m stuck here, I want to be able to Response.Write(stringbuilder)but it does not work, I have used the appropriate MIME types etc.However Response.TransmitFile() works when i just transmit an xml spreadsheet to browser.but Response.TransmitFile() takes a file as an argument, I can create a temp file write the created xml and then transmit but that just seems like a heavy overhead.Is there a way i can without creating a file, just transmitXml() using the stringbuilder contents and let the user save the spreadsheet.

View 1 Replies

C# - Response.TransmitFile Failing On First Download?

Oct 19, 2010

I have some code on an aspx page then when users loads the page it starts downloading a zip. Looks like this:

Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileSaveName);
Response.TransmitFile(zipPath);
Response.End();

The problem is the FIRST time this is hit I get the following error:

The zip file exists and is not in my wwwroot. If I refresh the page the file will download fine.

If I wrap the code in a Try Catch I get a System.Threading.Threadabort exception with the message:

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

NOTE: I actually get that exception every time. I guess its just to do with the Response.End

View 1 Replies

C# - Using Response.TransmitFile For Physical File Not Working?

Sep 27, 2010

I am trying to user the Response.TransmitFile() to prompt a download.

I have read a number of posts on the issue and based my method off Rick Strahl's blog [URL]

The only difference (that I can tell) is that I am targeting a physical file outside of the virtual directory.

This code is called in an ajaxified radgrid... I wonder if the response.transmitfile doesn't work with ajax calls?

Here is my code snippet:

[code]....

View 2 Replies

C# - Download .xlsx File Using Response.TransmitFile()?

Feb 16, 2010

I'm working on some code that generates an Excel spreadsheet server-side and then downloads it to the user. I'm using ExcelPackage to generate the file.

The generation is working just fine. I can open the generated files using Excel 2007 with no issues. But, I'm having trouble downloading the file with Response.TransmitFile().

Right now, I have the following code:

//Generate the file using ExcelPackage
string fileName = generateExcelFile(dataList, "MyReportData");
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.ContentType = "application/vnd.xls"
Response.Charset = "";
Response.TransmitFile(fileName);

When Excel 2007 opens the file downloaded as above, it gives the "file format doesn't match extension" warning. After clicking past the warning, Excel displays the raw xml contents of the file. If I change the file extension, like so Response.AddHeader("content-disposition", "attachment;filename=FileName.xlsx");
Excel 2007 gives an "Excel found unreadable content in the file" error, followed by a dialog that offers to locate a converter on the web. If I click "no" on this dialog, Excel is able to load the data.

I've also experimented with different MIME types, like application/vnd.ms-excel and application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, combined with file extensions of .xls and .xlsx. All combinations result in one of the two behaviors mentioned above. What is the correct combination of file extension and MIME type to use in this scenario? What else could cause this failure, other than an improper MIME type or extension? FYI, this is occurring with Visual Studio's built-in development web server. I haven't yet tried this with IIS.

View 1 Replies

Web Forms :: Response.TransmitFile Virtual Directory?

Sep 21, 2010

I can use Response.TransmitFile on my p.c. using VS2008 fine. But when I moved my code over to the server it won't work. Does the path have to be a virtual directory?

Lets assume you have 1 serverA with dir D:ExcelReports and another serverB thats the webserver. So to transfer a file you would need something like Response.TransmitFile(D:ExcelReports) ?

View 5 Replies

Web Forms :: How To Get Output HTML And TransmitFile As The Response To An Page

Nov 18, 2010

I'm trying to create an ASPX page that displays both an HTML message ("Please wait, your file transfer will begin momentarily") and also commence transmitting a file. I'm trying to avoid making the user open the page, and then clicking a Download buttonIt seems like this may be possible with a "Multipart/mixed" MIME type. Elsewhere I think I read that ASP.NET won't support this.

[Code]....

View 3 Replies

Error In Opening Word File Using Response.transmitfile?

Aug 27, 2010

I'm using asp.net 2.0 (VS2005) on a windows 2008 server.

i'm using MS Office 2007

when open word docuemnt from server using the following code -

Response.ContentType = "application/ms-word";
Response.AppendHeader("Content-Disposition", "attachment; filename= " + strDocName);
Response.TransmitFile(Server.MapPath(Defpath1));
Response.End();

it works fine when i run it in debug mode, but gives the following error when running through IIS Problems during Load Problems came up in the following areas during loadMissing file:
QTnicFLJ5JKZN8dyEzUEsK2jPtygbkw....

View 1 Replies

MVC :: Response.TransmitFile Zeroing Out Part Of The Result File?

Apr 8, 2010

I have an MVC controller that returns a file via the Response.TransmitFile

I works splendidly with small files. I noticed with larger files that after the client app receives the file, everything in the file from offset 10000 (hex) to the end of the file is zeroes. The resulting file is the correct length, its just that everything from 1000 on is zeroed out. BTW, WriteFile does the same exact thing.

View 6 Replies

Response.TransmitFile "Response.End" To Download File

Mar 31, 2010

I have following function which is called from a button click event

[Code]....

I am creating a zip file on the fly and wanted to download this file.Problem is that in Internet explorer when I click the button the download accelrator comes with file name as my page saying resume opendialogueif i click open then DAP window close and normal windows download manager comes but the event of my button fires multiple time?I don't know what to do with it

View 6 Replies

How To Use Httpcontext.response.transmitfile Or Other Function To Download A File From A Network?

Feb 25, 2010

Does anyone know how to use httpcontext.response.transmitfile or any other httpcontext function to download a file from a network drive instead of local drive? When I tried to use context.Response.TransmitFile(DownloadPath) to download files, the DownloadPath has to be local drive. If it's a network drive, the download failed.

View 2 Replies

Web Forms :: Client Download .csv File From Server Using Response.TransmitFile?

Sep 3, 2010

I am using the code below to enable clients to download a .csv file from my server.

The problem I am having is that when the file is saved to the client computer the file name is prefixed and suffixed with an underscore which leads to a file type of .csv_ which is obviously not a recognised file type.

How can I strip the underscores from the file name at the transmit stage?

Dim FileName As String = LstBox1.SelectedItem.ToString
Dim FilePath As String = "~/temp" & FileName
Response.Clear()
Response.ContentType = "text/csv"
Response.AddHeader("Content-Disposition", "attachment; filename=" & FileName & "")
Response.TransmitFile(FilePath)
Response.End()

View 9 Replies

Web Forms :: Response.TransmitFile --- Open A File From DataBase And Send It To Browser?

Aug 25, 2010

There are bunch of files stored in a DataBase as varbinary/image data. I am writing a program to extract these file data and send them to the browser so file can be opened or saved. My method is,

[code]....

BUT this method creates a file in the Server and then transmit to the browser. Means 100s of files will be created in the server when I open 100s of files with this process, naturally I want to avoid that.

Is it possible to do this without creating a copy of the file in the server ?

View 3 Replies

HttpHandlers / Modules :: Response.TransmitFile Stops Rest Of Page Execution?

Nov 12, 2010

i am trying to have a download on my page. I have implemented the code below but code execution stops after the download message pops up, even if i press save or cancel, nothing happens after that.

[Code]....

I have tried it a few times without Response.Clear() and Response.End() but it didn't make any difference to take these off. Is there anyway I can achieve this without having to using an IFrame?

View 7 Replies

C# - How To Lock A Private Static Field Of A Class In One Static Method

Mar 26, 2011

I have a private static field in my Controller class in an MVC web application.

I have a static method in that controller that assigns some value to that static field, I want to apply lock on that static field until some other instance method in the controller uses the value stored in the static field and then releases it.

DETAILS:

I have a controller named BaseController having a static ClientId field as follows and two methods as follows:-

public static string ClientId = "";
static void OnClientConnected(string clientId, ref Dictionary<string, object> list)
{
list.Add("a", "b");
// I want the ClientId to be locked here, so that it can not be accessed by other requests coming to the server and wait for ClientId to be released:-
BaseController.clientId = clientId;
}
public ActionResult Handler()
{
if (something)
{
// use the static ClientId here
}
// Release the ClientId here, so it can now be used by other web requests coming to the server.
return View();
}

View 1 Replies

AJAX :: Calling Webservices From Javascript: OnError Method, How To Identify The Method

Aug 26, 2010

I have an application that has JS calling ASMX files to do asyncronous requests, using ASP.NET AJAX.

The problem is that I use always the same "onError" function, and now I don't know how to identify the method that rised the error, and it is giving me a lot of problems to debug errors. The parameter "data" of the onError method does not give enough information, for
example:

en System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
en System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
en System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
en System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer)
en System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)
en System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)
en System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)
en System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)

Is there any way to know the webmethod that raised the error?

View 1 Replies

Calling Other Functions From A Shared (or Static) Function

Aug 20, 2010

I get this error: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.

Partial Class _Default
Inherits System.Web.UI.Page
<WebMethod()> _
Public Shared Function ParseData() As String
Dim value as string = GetValue()
End Function
Private Function GetValue() as String
Return "halp"
End Function
End Class

I know it has something to do with the fact that the first function is shared and the second function should probably be Public as well but I don't fully understand the reason behind it. Probably not relevant but I'm calling the web method from some javascript.

View 1 Replies

AJAX :: Calling A Regular Method From Within A Web Method?

Nov 1, 2010

i have a web service that i use for the AutoCompleteExtender, let the below code show you what i'm trying to do: [Code]....

this method is called, and when it calls the method inside the Patients Class, it gets lost. does that mean that i can't use 3-tier approach with web methods?

View 2 Replies

C# - Calling A Function After A Response.Redirect?

Aug 18, 2010

I have an ASP.NET site and I'm using jGrowl to display notifications to the user. This works great when the user stays on the same page, but what if I'm redirecting the user.

Example, AddPerson.aspx is a page for adding a new person. After a person is added the user is redirect to People.aspx. I'd like to then display the notification 'Josh Smith has been added.'.

I currently have a function in my Master page executing the javascript:

public void DisplayNotification(string html, string header)
{
string text = """ + html + """;
string options = "{ header: '" + header + "', life: 6000 }";
string scriptJGrowl = "$.jGrowl(" + text + ", " + options + ");";
ScriptManager.RegisterStartupScript(Page, GetType(), Guid.NewGuid().ToString(), scriptJGrowl, true);
}

How can I call this after a Response.Redirect?

View 4 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved