[英]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). 我使用的是http://www.thecssninja.com/javascript/gmail-upload上的源文件(所有代码都归功于CSSNinja)。 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.
我遇到的问题是,我基本上需要转换此JavaScript的一部分以与C#交互,问题是我真的不了解JavaScript。 I need to be able to save the files to specific directories with specific file names based on Session variables.
我需要能够基于Session变量将文件保存到具有特定文件名的特定目录中。 Current the script calls a simple php, and I don't know how to use PHP to interface with Sessions.
当前的脚本调用了一个简单的php,而我不知道如何使用PHP与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. 问题基本上是如何将上述内容转换为与C#中的类的接口,并传入上面的任何/一个参数。
PHP code PHP代码
<?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. JavaScript所做的只是向您发送两个特殊的HTTP标头和一个文件流作为请求主体,而PHP正在读取该标头。 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 . setRequestHeader(name, value)
使用名称name
和值value
设置普通的HTTP标头 ,而文件内容作为普通的请求正文发送。 All standard HTTP. 所有标准HTTP。
If you know your C# (and presumably ASP.NET), accessing two HTTP headers and reading the request stream should be no problem. 如果您知道C#(大概是ASP.NET),则访问两个HTTP标头并读取请求流应该没问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.