简体   繁体   中英

Calling C# from JavaScript with parameters

I'm using source files found at http://www.thecssninja.com/javascript/gmail-upload (all credit goes to CSSNinja for the code). And what I'm having an issue with is I basically need to convert a section of this JavaScript to interface with C#, the problem is i really don't know JavaScript. I need to be able to save the files to specific directories with specific file names based on Session variables. Current the script calls a simple php, and I don't know how to use PHP to interface with Sessions. Any help would be awesome.

xhr.open("POST", "upload.php");
xhr.setRequestHeader("If-Modified-Since", "Mon, 26 Jul 1997 05:00:00 GMT");
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader("X-File-Name", file.fileName);
xhr.setRequestHeader("X-File-Size", file.fileSize);
xhr.setRequestHeader("Content-Type", "multipart/form-data");
xhr.send(file);

The question is basically how to convert the above to interface with a class in C#, passing in any/one parameter that is above.

PHP code

<?php
$headers = getallheaders();
// create the object and assign property
$file = new stdClass;
$file->name = basename($headers['X-File-Name']);
$file->size = $headers['X-File-Size'];
$file->content = file_get_contents("php://input");

// if everything is ok, save the file somewhere
echo $file->content;    

?>

All right, enough comments...

All that JavaScript is doing is sending you two special HTTP headers and a file stream as the request body, and the PHP is reading that. Nothing more.

setRequestHeader(name, value) sets a normal HTTP header with the name name and the value value , while the file content is sent as a normal request body . All standard HTTP.

If you know your C# (and presumably ASP.NET), accessing two HTTP headers and reading the request stream should be no problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM