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

Dani AI

Generated

Short answer: you cannot force a browser to write a file directly to a specific path on a visitor's C: drive or prevent the user from renaming the file in the Save dialog. As noted, HTTP gives the server a way to suggest a filename (via Content-Disposition) but the client controls where and under what name the file is saved. For a reliable, user-friendly solution, create the file on the server and stream it to the browser with a correct Content-Disposition (server provides the default name), or generate the file client-side so the browser handles the download immediately.

Practical tips and fixes for your code flow:

  • When you check/create the server folder, operate on the directory path, not the full filename. Use Path.GetDirectoryName and Path.Combine to build paths safely.
  • Stream the completed file to the client and set Content-Length and a quoted filename so browsers get a sensible default name.
  • If you want a purely client-side download (no temp server file), use an HTML5 Blob and the download attribute (works in modern browsers).

Example (VB.NET server-stream pattern — stream a server file and suggest a filename):

Dim serverPath = Server.MapPath("~/TextFiles/Branch/" & fileName & ".txt")
Response.Clear()
Response.ContentType = "text/plain"
Response.AddHeader("Content-Disposition", "attachment; filename=""" & Path.GetFileName(serverPath) & """")
Response.AddHeader("Content-Length", New FileInfo(serverPath).Length.ToString())
Response.TransmitFile(serverPath)
Response.Flush()
Response.End()

Example (client-side, creates and downloads a text file without server temp files):

const blob = new Blob([text], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url; a.download = 'Name-20251112.txt';
document.body.appendChild(a); a.click();
URL.revokeObjectURL(url); a.remove();

If you control client machines (intranet), you can deploy a signed client app, ClickOnce, or script with elevated privileges to write to a fixed path — otherwise assume the user will always be able to choose or rename the file.

Recommended Answers

All 2 Replies

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.