I want to create text file on clients local drive.Please help.
Please support our ASP.NET advertiser: DiscountASP.NET – 3 Months Free on ASP.NET 3.5/2.0 Web Hosting
![]() |
•
•
Posts: 3
Reputation:
Solved Threads: 0
Hi,
I have a problem which I have failed to solve.If you have time please help.I am generating text file on button's onclick.This text file is being
created on server .I want to create on the local machine (clients C:/)
See below code snippets.
***********************************************************************
Imports System.IO
Dim sw As StreamWriter
Dim strRecord,filenam As String
Protected Sub BtnTextFile_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles BtnTextFile.Click
filenam = "c:\TextFiles\Name1\" & "Name-" &
DateTime.Today.Day.ToString("00") & DateTime.Today.ToString("MMM") &
DateTime.Today.Year.ToString() & DateTime.Now.Hour.ToString("00") +
DateTime.Now.Minute.ToString("00") + DateTime.Now.Second.ToString("00") &
".txt"
If Directory.Exists(filenam) = False Then
Directory.CreateDirectory("c:\TextFiles\Name1")
End If
sw = File.CreateText(filenam)
sSqlString = "Select * from mstdata WHERE Convert(varchar,CreatedOn,101)
BETWEEN '" & Format(Convert.ToDateTime(txtfrmdt.Text), "MM/dd/yyyy") & "'
And '" & Format(Convert.ToDateTime(txttodt.Text), "MM/dd/yyyy") & "' order
by CreatedOn asc"
Dr = objservice.ReturnDataReader(sSqlString)
Do While Dr.Read
Dim saddress2 As String = ""
If String.IsNullOrEmpty((Dr(5)).ToString) = False Then
saddress2 = Trim(Dr(5))
Else
saddress2 = ""
End If
Dim cby As String = ""
If String.IsNullOrEmpty((Dr(10)).ToString) = False Then
cby = Trim(Dr(10))
Else
cby = ""
End If
strRecord = Trim(Dr(0)) & "|" & Trim(Dr(1)) & "|" & Trim(Dr(2)) & "|" &
Trim(Dr(3)) & "|" & Trim(Dr(4)) & "|" & saddress2 & "|" & Trim(Dr(6)) &
"|" & Trim(Dr(7)) & "|" & Trim(Dr(8)) & "|" & Trim(Dr(9)) & "|" & cby &
"|" & Trim(Dr(11)) & vbNewLine
sw.Write(strRecord)
Loop
sw.Close()
End Sub
******************************************************************
Regards
I have a problem which I have failed to solve.If you have time please help.I am generating text file on button's onclick.This text file is being
created on server .I want to create on the local machine (clients C:/)
See below code snippets.
***********************************************************************
Imports System.IO
Dim sw As StreamWriter
Dim strRecord,filenam As String
Protected Sub BtnTextFile_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles BtnTextFile.Click
filenam = "c:\TextFiles\Name1\" & "Name-" &
DateTime.Today.Day.ToString("00") & DateTime.Today.ToString("MMM") &
DateTime.Today.Year.ToString() & DateTime.Now.Hour.ToString("00") +
DateTime.Now.Minute.ToString("00") + DateTime.Now.Second.ToString("00") &
".txt"
If Directory.Exists(filenam) = False Then
Directory.CreateDirectory("c:\TextFiles\Name1")
End If
sw = File.CreateText(filenam)
sSqlString = "Select * from mstdata WHERE Convert(varchar,CreatedOn,101)
BETWEEN '" & Format(Convert.ToDateTime(txtfrmdt.Text), "MM/dd/yyyy") & "'
And '" & Format(Convert.ToDateTime(txttodt.Text), "MM/dd/yyyy") & "' order
by CreatedOn asc"
Dr = objservice.ReturnDataReader(sSqlString)
Do While Dr.Read
Dim saddress2 As String = ""
If String.IsNullOrEmpty((Dr(5)).ToString) = False Then
saddress2 = Trim(Dr(5))
Else
saddress2 = ""
End If
Dim cby As String = ""
If String.IsNullOrEmpty((Dr(10)).ToString) = False Then
cby = Trim(Dr(10))
Else
cby = ""
End If
strRecord = Trim(Dr(0)) & "|" & Trim(Dr(1)) & "|" & Trim(Dr(2)) & "|" &
Trim(Dr(3)) & "|" & Trim(Dr(4)) & "|" & saddress2 & "|" & Trim(Dr(6)) &
"|" & Trim(Dr(7)) & "|" & Trim(Dr(8)) & "|" & Trim(Dr(9)) & "|" & cby &
"|" & Trim(Dr(11)) & vbNewLine
sw.Write(strRecord)
Loop
sw.Close()
End Sub
******************************************************************
Regards
You cannot save a file directly to the users machine, in an HTTP web environment you do not have access to a users file system and no user would want you to have that ability either. Having created the file on the server you need to give your user a link to click or redirect to a download page for that file, and let the user save it to their file system.
•
•
Posts: 3
Reputation:
Solved Threads: 0
While I am Downloading ,One Dialogbox will open.When user will be saving file name...that time I want to restrict user to change the file name ..I have written code below for downloading file.
Dim fs As System.IO.FileStream = Nothing
fs = File.Open(Server.MapPath("TextFiles/Branch/" + filenam + ".txt"), System.IO.FileMode.Open)
Dim btFile(fs.Length) As Byte
fs.Read(btFile, 0, fs.Length)
fs.Close()
With Response
.AddHeader("Content-disposition", "attachment;filename=" & filenam)
.ContentType = "application/octet-stream"
.BinaryWrite(btFile)
.End()
End With
With Regards,
mishrap
Dim fs As System.IO.FileStream = Nothing
fs = File.Open(Server.MapPath("TextFiles/Branch/" + filenam + ".txt"), System.IO.FileMode.Open)
Dim btFile(fs.Length) As Byte
fs.Read(btFile, 0, fs.Length)
fs.Close()
With Response
.AddHeader("Content-disposition", "attachment;filename=" & filenam)
.ContentType = "application/octet-stream"
.BinaryWrite(btFile)
.End()
End With
With Regards,
mishrap
![]() |
Similar Threads
Other Threads in the ASP.NET Forum
- Help with appending text file (VB.NET)
- turning userinput into a text file (C)
- How do you create a text file? (C)
- Generate file in memory to save to local disk (ASP.NET)
- Output in Text file-How to apply fprintf()? (C)
- Create stats from a text file (Java)
- 10 line text file (Java)
Other Threads in the ASP.NET Forum
- Previous Thread: Table Control, wats wrong
- Next Thread: While I am Downloading ,One Dialogbox will open.When user will be saving file name...
•
•
•
•
Views: 2105 | Replies: 2 | Currently Viewing: 1 (0 members and 1 guests)






Linear Mode