简体   繁体   English

如何在jQuery中通过Ajax将文件保存以发送到服务器?

[英]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.

相关问题 如何通过jQuery / ajax将下拉样式值传递给我的php文件? - How can I pass my dropdown style value on to my php file via jQuery/ajax? 如何从 JQuery Ajax 向后端 C# 发送文件和字符串? - How can I send a file and a string to my backend C# from JQuery Ajax? 如何通过jQuery $ .ajax()将JSON字符串发布到服务器? - How can I post a JSON string to the server via jQuery $.ajax()? 如何通过 jQuery AJAX 请求读取文件? - How can I read a file via a jQuery AJAX request? 我可以从浏览器控制台(通过AJAX)将信息发送到本地/远程主机上的php文件吗? - Can I send information from browser console (via AJAX) to my php file on local/remote host? 如何通过 ajax jquery/javascript 向服务器发送 OPTIONS 方法调用? - How to send OPTIONS Method call to server via ajax jquery/javascript? 如何通过AJAX将数据从外部站点发送到我的服务器? - How to send data from outside site to my server via AJAX? 如何将文件内容发送到服务器? - How can I send the contents of a file to my server? 如果我使用 jQuery 的 ajax 方法将 CSV 文件发送到服务器,文件将位于我的 Node.js + Express 服务器上的什么位置? - Where would a file be located on my Node.js + Express server if I send a CSV file to the server using jQuery's ajax method? 如何使用 Ajax 发送文件? - How I Can send a file with Ajax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM