How to create a file in local pc using asp.net

Hello!!! I am creating a file to be placed in local pc and not in the server using asp.net. The next code is working fine to create the file in the server, but i want it in local pc.
If i change the path to local computer "access is denied".
How can i do this??

this is the current code iam using:


Dim fp As StreamWriter

Try
fp = File.CreateText(server.mappath(".\DecGenerator\") + V52.text + "_" + V60.text + ".x12")
fp.WriteLine(edistring1)
result3.Text = "Declaración archivada y documento EDI generado exitosamente!"
fp.Close()
Catch err As Exception
result3.Text = "Declaración y documento EDI no fueron generados: " + err.ToString()
Finally

End Try

robossaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sagacitysolutionsCommented:
You cannot write the file to the client

You can write the file to the server and then provide a link to the server file so the client can download
sagacitysolutionsCommented:
Dim fp As StreamWriter
Dim fn As String

Try
fn = server.mappath(".\DecGenerator\") + V52.text + "_" + V60.text + ".x12"
fp = File.CreateText(fn)
fp.WriteLine(edistring1)
result3.Text = "Declaración archivada y documento EDI generado exitosamente!" + "<a href=""" + fn + """>Download</a>"
fp.Close()
Catch err As Exception
result3.Text = "Declaración y documento EDI no fueron generados: " + err.ToString()
Finally

End Try
emblueCommented:
It is not possible for the server to write a file to the client.  That is a violation of HTTP security, no browser would permit that.

The only option is as sagacitysolutions  said - to write the file on the server and provide a download link.
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

robossaAuthor Commented:
thanks. I introduced fn , but the file does not dowload. Do I need permission to do that from the hosting service??
robossaAuthor Commented:
In addition, I will need to delete the created file after download.
emblueCommented:
I do not have any experience with ASP specfically, so I'm not farmiliar with the syntax - but can you specify MIME headers?

In Perl, for example, if I sent the "Content-type:application/octet-stream" header before the content, and then rather than saving the contents of your file, you printed it to the browser window, the browser would prompt the user to save the file rather than display it.

That might accomplish your goal.
sagacitysolutionsCommented:
Is results3 a textbox or a label
robossaAuthor Commented:
results3  is a label
sagacitysolutionsCommented:
Open the page in a web browser then do a View Source
paste the source here
robossaAuthor Commented:
It seems the link for download is ok and the created file as well. Please don't forget to include the code for deleting the file after deletion. thanks.


 <table id="tableformat" width="800" align="center" border="0" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0">
      <tr>
            <td>

                                    <table width="800" align="center" border="0">
                  <tr class="subtitle2">
                        <td align="middle">
                                        <span id="result3">Declaración archivada y documento EDI generado exitosamente!-<a href="d:\html\users\linklogisticonet\html\software\imports\DecGenerator\42962_01324566.x12">Download</a></span></td>
                  </tr>
                  <tr class="2">
                        <td class="r" colspan="2">
                                       <input type="image" name="reset2" id="reset2" src="../images/topLimpiar.jpg" alt="" border="0" />
                                     
                                     
                                       
                                   
                                        </td>
                  </tr>
            </table>
sagacitysolutionsCommented:
Lets get the link working first, then we will work on deleting the file

Try This


Dim fp As StreamWriter
Dim fn As String
Dim fsn As String

Try
fn = server.mappath(".\DecGenerator\") + V52.text + "_" + V60.text + ".x12"
fsn = ".\DecGenerator\" + V52.text + "_" + V60.text + ".x12"

fp = File.CreateText(fn)
fp.WriteLine(edistring1)
result3.Text = "Declaración archivada y documento EDI generado exitosamente!" + "<a href=""" + fsn + """>Download</a>"
fp.Close()
Catch err As Exception
result3.Text = "Declaración y documento EDI no fueron generados: " + err.ToString()
Finally

End Try
robossaAuthor Commented:
Emblue!!! the mime  headers is Content-type: application/EDI-X12
robossaAuthor Commented:
Great!!! It is working. When click on download, the window shows the file content .
 However, what I want is to receive a promt "where do you want to save it", then I can
save it in the local pc. Once that have happened, the file must be deleted in the server. thanks.
sagacitysolutionsCommented:
Add this to the <HEAD> Section of the Page
<script>
 function forcedown(x){
    myTempWindow = window.open(x);
    myTempWindow.document.execCommand("SaveAs",null,x);
    myTempWindow.close();
    location.href=location.href+"?deletefile=true"
}
</script>


Then modify the following Line to read as follows

result3.Text = "Declaración archivada y documento EDI generado exitosamente!" +
"<a href=""javascript://"" onclick=""forcedown(fsn)" + fsn + ";"">Download</a>"

Let me know if that works
robossaAuthor Commented:
It seems the download link is not well formatted: this is what it shows view source:

<a href="javascript://" onclick="forcedown(fsn).\DecGenerator\42962_01324566.x12;">Download</a></span></td>
sagacitysolutionsCommented:
Change as follows:

result3.Text = "Declaración archivada y documento EDI generado exitosamente!" +
"<a href=""javascript://"" onclick=""forcedown(" + fsn + ");"">Download</a>"
robossaAuthor Commented:
Its not working eithe. view source is

<a href="javascript://" onclick="forcedown(.\DecGenerator\42962_01324566.x12);">Download</a></span></td>
sagacitysolutionsCommented:
Well that looked better

Try this
result3.Text = "Declaración archivada y documento EDI generado exitosamente!" +
"<a href=""javascript://"" onclick=""forcedown('" + fsn + "');"">Download</a>"


After you click the hyperlink and save or cancel your address bar should have ?deletefile appended to the end
robossaAuthor Commented:
It opens a window and close it inmidiatelly. Thus "save as window" is not promtp.

The download link view source is ok:

<a href="javascript://" onclick="forcedown('/DecGenerator/42962_01324566.x12');">Download</a></span></td>

It seems is something wiht the javascript???
sagacitysolutionsCommented:
Replace the script with this

<script>
 function forcedown(x){
   var cmd
    myTempWindow = window.open(x);
    cmd = myTempWindow.document.execCommand("SaveAs",null,x);
    myTempWindow.close();
    location.href=location.href+"?deletefile=true"
}
</script>
robossaAuthor Commented:
It worked fine, However the file is saved in html format, the content has on top this code I want to eliminate:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252"></HEAD>
<BODY onunload=""><PRE>.

EDI CODE I AM INTERESTED IN


at the bottom of the document it have this code

</PRE></BODY></HTML>

thanks. Roberto
robossaAuthor Commented:
Sorry!! I forgot to tell you that the file in the server was not deleted after download. What would be wrong with that. Thanks/
sagacitysolutionsCommented:
After you save the document you should return to your exiting browser and the URL should have ?deletefile=true at the end.

Add code in your Page_Load Event To check for the Querystring("deletefile") = "true"

If so then delete the file

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
robossaAuthor Commented:
the file is saved in html format, would you please let me know how to save it plain text. Thnaks. Robossa
sagacitysolutionsCommented:
Try This
cmdExec = myTempWindow.document.execCommand("SaveAs",true,"edi.txt");
robossaAuthor Commented:
It works, however variable "x" brings the name of the edi document (32423_2334324.x12) so the user will not know what document is saving in local pc. thanks.
sagacitysolutionsCommented:
Im not sure what you mean, can you explain a little more
robossaAuthor Commented:
Thnaks a lot yuu have been excelent. Roberto
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.