need help, cannot write a file on Server

0
0

Hi,

I’m rum´nning circles.  I want to write a xml string into a file on the server, using following code.

*************

var data = ‘<Shape label=”Check Inventory” description=”” href=”” id=”11″>
<mxCell style=”rhombus” parent=”3″ vertex=”1″>
<mxGeometry x=”240″ y=”50″ width=”92″ height=”50″ as=”geometry”/>
</mxCell>
</Shape>”‘

filename = “xmlfiles/testfile.xml”

request = new XMLHttpRequest();
request.open(“POST”, filename, true);

request.setRequestHeader(“Content-type”, “plain/text; charset=utf-8”);
request.withCredentials = false;

request.send(data);

************

I get no errors, in Firefox looks everything ok, but on server there is no file…..

 

Any hint is very appreciated

Regards

Johann

  • You must to post comments
0
0

Hi,

I have installed on the webserver machine the node.js and tested the install samples successfully. What I don’t know is, what do I have to configure in my VS project, to get the things running.

What I have done till now:

I created a file nodeutils.js and created inside following function:

function write_node(data, filepath, filename) {
console.log(data);
var http = require(‘http’);
var fs = require(‘fs’);
var file = filepath; // including filename
console.log(‘file: ‘, file);
http.createServer(function (req, res) {
fs.writeFile(file, data, function (err) {
res.writeHead(200, { ‘Content-Type’: ‘text / html’ });
res.write(data);
res.end();
if (err) throw err;
console.log(‘File was created successfully’);
}).listen(8080);

})
};

 

In the project web.config file I added following line in the

<handlers>

<add name=”iisnode” path=”nodeutils.js” verb=”*” modules=”iisnode” />

</handlers>

 

And finally, from the html file I make a call to the function write_node(data, filepath, filename)

What is wrong with this stuff?

  • You must to post comments
0
0

Hi Johann,

 

Does the receiving server have a php, Node.js, or another backend script to receive the file through a POST request?

If you want to use FTP this might be helpful: https://www.aspsnippets.com/Articles/Uploading-Files-to-FTP-Server-programmatically-in-ASPNet-using-C-and-VBNet.aspx

 

If you are trying to write to the server that the Wisej app is hosted on you can use something like:

using (System.IO.StreamWriter w = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\text.txt", true))
{
    w.WriteLine("Hello World"); // Write the text
}

Looking up this kind of thing might be helpful: https://stackoverflow.com/questions/51141428/write-to-a-file-c-sharp-asp-net-on-button-click

You can also use this.evalAsync(); to receive the results of your JavaScript code in C#.

Please let me know if you have any more questions!

I hope these help.

Best,
Levie

  • Johann Gregorich
    Hello Levie, meanwhile I have installed Node.js on the server. My function called from client looks like this: function write_node(data, filepath, filename) { console.log(data); var fs = require(‘fs’); var file = filepath; // including filename console.log(‘file: ‘, file); fs.writeFile(file, data, function (err) { if (err) throw err; console.log(‘File was created successfully’); }); }; but I get a reference error in the line var fs = require(‘fs’); because this function should be executed on server, but I don’t know how to tell this to the server… Do you have a hint/sample how to do this? Regards Johann
  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.