[英]How can I send a file, for saving, to my server, via ajax, in jQuery?
First off, you'll have to forgive my lack of knowledge in submitting forms with Ajax; 首先,您必须原谅我缺乏使用Ajax提交表单的知识; as a result I may ask a stupid question or two as I try to figure out exactly what I can and can't do with this. 结果,我可能会问一个或两个愚蠢的问题,因为我试图弄清楚我能做什么和不能做什么。
Secondly, I know how to do simple Ajax requests using jQuery that usually look a lot like this: 其次,我知道如何使用通常看起来像这样的jQuery执行简单的Ajax请求:
$.ajax({
url: "/SomeDir/SomePage.cshtml",
async: true, //sometimes this is false, depending on how I want this to operate.
type: "POST", //most of the time this is 'GET' but it depends on whether server-side changes will be taking place or sensitive data will be passed.
dataType: "html", //This has the potential to change a lot.
data: { someVar: someVarValue }, //any number of name/value pairs to send to the server with the request.
success: function (response) {
//success callback function code here.
},
error: function (jqXHR, textStatus, error) {
//code to run if error occurs (usually displaying text showing the 'textStatus' and 'error' values.
}
});
Okay, so now that I've shown you how I normally perform an Ajax request (just the standard jQuery way) you'll know where I am on this part of it. 好的,现在我已经向您展示了我通常如何执行Ajax请求(只是标准的jQuery方式),您将知道我在此部分中的位置。
Now for the my goal: 现在我的目标是:
I have a simple <input id="inFile" name="inFile" type="file" />
with a home-made <button>
underneath. 我有一个简单的<input id="inFile" name="inFile" type="file" />
,下面有一个自制的<button>
。 When the button is clicked, what I need is for Ajax to take that file to the server, save it in a specified directory (I don't need instructions for that part), then re-update the page (I don't need help with that part either). 单击按钮后,我需要的是Ajax将该文件带到服务器,将其保存在指定的目录中(我不需要该部分的说明),然后重新更新页面(我不需要要么帮助该部分)。
Pretty much, what I need to know is how to go from client-side to server-side with the file intact (with or without using a form) while not refreshing the page. 几乎,我需要知道的是如何在不刷新页面的情况下,从客户端到服务器端完整保存文件(使用或不使用表单)。
I have thought that perhaps I lack enough understanding of the Ajax type: "POST"
that it may inherently provide some of the functionality I am asking for and I don't even know it. 我以为也许我对Ajax type: "POST"
缺乏足够的理解type: "POST"
它可能会固有地提供我要的某些功能,而我什至不知道。
I know I can send any data I want with the "data" property (is it called a property?) of the $.ajax();
我知道我可以使用$.ajax();
的“数据”属性(称为属性?)发送任何我想要的数据$.ajax();
function, but if that's the way it should be done, I don't know how to save the file into a variable/object/collection for sending with the $.ajax()
function. 函数,但是如果那是应该这样做的方式,我不知道如何使用$.ajax()
函数将文件保存到变量/对象/集合中进行发送。
Sorry for my lack of knowledge on this, but I have never done this particular task with Ajax before, and I can't seem to find anything on this either. 抱歉,我对此一无所知,但是我以前从未在Ajax上完成过此特定任务,而且我似乎也找不到任何东西。
In Short: 简而言之:
All I really need is to get the file from the client-side to the server side so that I can save it with C#'s System.IO
我真正需要的只是从客户端到服务器端获取文件,以便可以使用C#的System.IO
保存它。
I would use HTML5 and pure javascript or a jquery plugin (There are many good cross-browser ones for downloading files that can be adapted, you really just need to format it here) to create fallbacks for older browsers. 我会使用HTML5和纯javascript或jquery插件(有很多很好的跨浏览器插件,可以下载可以改编的文件,您实际上只需要在此处设置其格式)即可为较旧的浏览器创建备用。 Then send data to a server script using ajax where you can write the posted data to a file on the server-side. 然后使用ajax将数据发送到服务器脚本,您可以在其中将发布的数据写入服务器端的文件中。
It's not the time for me to be awake so forgive me for not giving more detail, get in touch if you are still having trouble. 现在不是时候让我醒来,请原谅我没有提供更多细节,如果仍然遇到问题,请与我们联系。
Note:I have no experience with web pages but since it uses c# I'd assume it's similar to MVC so once you post the data to the right script/function with ajax, you should have no problem writing it to a file despite the language. 注意:我没有使用网页的经验,但是由于它使用c#,所以我认为它与MVC类似,因此,一旦使用ajax将数据发布到正确的脚本/函数中,尽管使用了该语言,也可以毫无问题地将其写入文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.