sarpay
11/18/2014 - 10:29 PM

[.net] [ajax] Download sample

[.net] [ajax] Download sample

******************
*** index.html ***
******************

<iframe id="downloader" src="about:blank" width="0" height="0" frameborder="0" scrolling="no"></iframe>


**************
*** app.js ***
**************

function downloadAllUsers() {
  var ajaxCall = makeAjaxCall(FUNC_CREATE_USERS_EXPORT_FILE, paramsData);
  ajaxCall.success(function (serverResponse) {
    var responseData = serverResponse.d[0];
    if (responseData.Server_Response == 'ok') {
      document.getElementById('downloader').src = 'downloader.aspx?filename=' + responseData.ZipFileName;
    }
  });
}


*********************
*** webservice.vb ***
*********************

Protected Const TempFilesDirectory As String = "temp/"

<WebMethod()> _
Public Function CreateUsersExportFile( _
    ByVal pollGuid As String _
  ) As Object()

  Dim list As New List(Of Object)()
  Dim dict As New Dictionary(Of String, Object)()
  
  '*** Generate Names
  Dim nowTime As DateTime = DateTime.UtcNow()
  Dim playersFileName As String = _
    nowTime.Year.ToString() + "-" + _
    nowTime.Month.ToString() + "-" + _
    nowTime.Day.ToString() + "-" + _
    nowTime.Hour.ToString() + "-" + _
    nowTime.Minute.ToString() + "-" + _
    nowTime.Second.ToString() + "-UTC.csv"
  Dim playersFilePath As String = Path.Combine(Server.MapPath(TempFilesDirectory), playersFileName)

  Dim zipFileName As String = pollGuid + ".zip"
  Dim zipFilePath As String = Path.Combine(Server.MapPath(TempFilesDirectory), zipFileName)
  
  '*** Create CSV File
  If playerStrBuilder.Length > 0 Then
    File.WriteAllText(playersFilePath, playerStrBuilder.ToString(), UnicodeEncoding.UTF8)
    playerStrBuilder.Length = 0 '*** reset string builder (free up memory)
  End If

  '*** Create the list of files to be packaged
  Dim filesToInclude(0) As String
  filesToInclude(0) = playersFilePath

  '*** Zip the files and save to disk
  Using zip As New ZipFile()
    zip.AddFiles(filesToInclude, "csv")
    zip.Save(zipFilePath)
    End Using

  '*** Clean up Temp Files.
  For i = 0 To filesToInclude.GetUpperBound(0)
    If File.Exists(filesToInclude(i)) Then
      File.Delete(filesToInclude(i))
    End If
  Next

  dict.Add("Server_Response", "ok")
  dict.Add("ZipFileName", zipFileName)
  
  list.Add(dict)

  Return list.ToArray()

End Function


**************************
*** downloader.aspx.vb ***
**************************

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

  If Not Request.QueryString("filename") Is Nothing Then

    Dim fileName As String = Server.UrlDecode(Request.QueryString("filename"))
    Dim filePath As String = Path.Combine(Server.MapPath("temp/"), fileName)

    Dim file As FileInfo = New FileInfo(filePath)
    If file.Exists Then
      Response.Clear()
      Response.Buffer = False
      'Attachment - forces download, inline - forces the browser to open the content inline if possible.
      Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name)
      Response.AddHeader("Content-Length", file.Length.ToString())
      Response.ContentType = "application/x-zip-compressed"
      Response.WriteFile(file.FullName)
      Response.Flush()
      Response.Close()
      file.Delete()
    End If

  End If

End Sub