当前位置:首页 » 文件管理 » 表单提交文件上传

表单提交文件上传

发布时间: 2022-08-21 15:11:01

‘壹’ ajax怎样提交form表单与实现文件上传

Ajax 提交form方式可以将form表单序列化 然后将数据通过data提交至后台,例如:

‘贰’ 如何使用multipart/form-data格式上传文件

在网络编程过程中需要向服务器上传文件。Multipart/form-data是上传文件的一种方式。

Multipart/form-data其实就是浏览器用表单上传文件的方式。最常见的情境是:在写邮件时,向邮件后添加附件,附件通常使用表单添加,也就是用multipart/form-data格式上传到服务器。


表单形式上传附件

具体的步骤是怎样的呢?

首先,客户端和服务器建立连接(TCP协议)。

第二,客户端可以向服务器端发送数据。因为上传文件实质上也是向服务器端发送请求。

第三,客户端按照符合“multipart/form-data”的格式向服务器端发送数据。

既然Multipart/form-data格式就是浏览器用表单提交数据的格式,我们就来看看文件经过浏览器编码后是什么样子。

这行指出这个请求是“multipart/form-data”格式的,且“boundary”是 “---------------------------7db15a14291cce”这个字符串。

不难想象,“boundary”是用来隔开表单中不同部分数据的。例子中的表单就有 2 部分数据,用“boundary”隔开。“boundary”一般由系统随机产生,但也可以简单的用“-------------”来代替。

实际上,每部分数据的开头都是由"--" + boundary开始,而不是由 boundary 开始。仔细看才能发现下面的开头这段字符串实际上要比 boundary 多了个 “--”

紧接着 boundary 的是该部分数据的描述。

接下来才是数据。


“GIF”gif格式图片的文件头,可见,unknow1.gif确实是gif格式图片。

在请求的最后,则是 "--" + boundary + "--" 表明表单的结束。

需要注意的是,在html协议中,用 “ ” 换行,而不是 “ ”。

下面的代码片断演示如何构造multipart/form-data格式数据,并上传图片到服务器。

//---------------------------------------

// this is the demo code of using multipart/form-data to upload text and photos.

// -use WinInet APIs.

//

//

// connection handlers.

//

HRESULT hr;

HINTERNET m_hOpen;

HINTERNET m_hConnect;

HINTERNET m_hRequest;

//

// make connection.

//

...

//

// form the content.

//

std::wstring strBoundary = std::wstring(L"------------------");

std::wstring wstrHeader(L"Content-Type: multipart/form-data, boundary=");

wstrHeader += strBoundary;

HttpAddRequestHeaders(m_hRequest, wstrHeader.c_str(), DWORD(wstrHeader.size()), HTTP_ADDREQ_FLAG_ADD);

//

// "std::wstring strPhotoPath" is the name of photo to upload.

//

//

// uploaded photo form-part begin.

//

std::wstring strMultipartFirst(L"--");

strMultipartFirst += strBoundary;

strMultipartFirst += L" Content-Disposition: form-data; name="pic"; filename=";

strMultipartFirst += L""" + strPhotoPath + L""";

strMultipartFirst += L" Content-Type: image/jpeg ";

//

// "std::wstring strTextContent" is the text to uploaded.

//

//

// uploaded text form-part begin.

//

std::wstring strMultipartInter(L" --");

strMultipartInter += strBoundary;

strMultipartInter += L" Content-Disposition: form-data; name="status" ";

std::wstring wstrPostDataUrlEncode(CEncodeTool::Encode_Url(strTextContent));

// add text content to send.

strMultipartInter += wstrPostDataUrlEncode;

std::wstring strMultipartEnd(L" --");

strMultipartEnd += strBoundary;

strMultipartEnd += L"-- ";

//

// open photo file.

//

// ws2s(std::wstring)

// -transform "strPhotopath" from unicode to ansi.

std::ifstream *pstdofsPicInput = new std::ifstream;

pstdofsPicInput->open((ws2s(strPhotoPath)).c_str(), std::ios::binary|std::ios::in);

pstdofsPicInput->seekg(0, std::ios::end);

int nFileSize = pstdofsPicInput->tellg();

if(nPicFileLen == 0)

{

return E_ACCESSDENIED;

}

char *pchPicFileBuf = NULL;

try

{

pchPicFileBuf = new char[nPicFileLen];

}

catch(std::bad_alloc)

{

hr = E_FAIL;

}

if(FAILED(hr))

{

return hr;

}

pstdofsPicInput->seekg(0, std::ios::beg);

pstdofsPicInput->read(pchPicFileBuf, nPicFileLen);

if(pstdofsPicInput->bad())

{

pstdofsPicInput->close();

hr = E_FAIL;

}

delete pstdofsPicInput;

if(FAILED(hr))

{

return hr;

}

// Calculate the length of data to send.

std::string straMultipartFirst = CEncodeTool::ws2s(strMultipartFirst);

std::string straMultipartInter = CEncodeTool::ws2s(strMultipartInter);

std::string straMultipartEnd = CEncodeTool::ws2s(strMultipartEnd);

int cSendBufLen = straMultipartFirst.size() + nPicFileLen + straMultipartInter.size() + straMultipartEnd.size();

// Allocate the buffer to temporary store the data to send.

PCHAR pchSendBuf = new CHAR[cSendBufLen];

memcpy(pchSendBuf, straMultipartFirst.c_str(), straMultipartFirst.size());

memcpy(pchSendBuf + straMultipartFirst.size(), (const char *)pchPicFileBuf, nPicFileLen);

memcpy(pchSendBuf + straMultipartFirst.size() + nPicFileLen, straMultipartInter.c_str(), straMultipartInter.size());

memcpy(pchSendBuf + straMultipartFirst.size() + nPicFileLen + straMultipartInter.size(), straMultipartEnd.c_str(), straMultipartEnd.size());

//

// send the request data.

//

HttpSendRequest(m_hRequest, NULL, 0, (LPVOID)pchSendBuf, cSendBufLen)

‘叁’ SpringMVC表单提交时,多文件上传和单个文件上传有些什么区别

基于Spring3 MVC实现基于form表单文件上传
一:杂项准备
环境搭建参考这里
二:前台页面
根据RFC1867,只要在提交form表单中声明提交方法为POST,enctype属
性声明为multipart/form-data, action声明到要提交的url即可。具体如下:

三:spring配置
使用spring3的MultipartHttpReqest来接受来自浏览器的发送的文件内容。
需要配Multipart解析器在express-servlet.xml中。内容如下:

同时还需要在maven的pom.xml文件添加apachefileupload与common-io两个包。

四:Controller中方法实现

[java] view plain
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public ModelAndView getUploadFile(HttpServletRequest request, HttpServletResponse response) {
System.out.println("fucking spring3 MVC upload file with Multipart form");
String myappPath = request.getSession().getServletContext().getRealPath("/");
try {
if (request instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
System.out.println("fucking spring3 MVC upload file with Multipart form");
// String myappPath = multipartRequest.getServletContext().getRealPath("/");
// does not work, oh my god!!
MultipartFile file = multipartRequest.getFiles("userfile1").get(0);
long size = file.getSize();
byte[] data = new byte[(int) size];
InputStream input = file.getInputStream();
input.read(data);

// create file, if no app context path, will throws access denied.
// seems like you could not create any file at tomcat/bin directory!!!
File outFile = new File(myappPath + File.separator + file.getOriginalFilename());
if(!outFile.exists()) {
outFile.createNewFile();
System.out.println("full path = " + outFile.getAbsolutePath());
} else {
System.out.println("full path = " + outFile.getAbsolutePath());
}
FileOutputStream outStream = new FileOutputStream(outFile);

outStream.write(data);
outStream.close();
input.close();
}
} catch (Exception e) {
e.printStackTrace();
}

return new ModelAndView("welcome");
}

‘肆’ java表单提交里面的文件上传,用同步的方式好还是用异步的好啊

你好,很高兴回答你的问题。
这里做成异步比较好。如果同步的话,遇到文件大的话,表单提交会很慢,体验非常不好。
如果有帮助到你,请点击采纳。

‘伍’ ajax怎么提交带文件上传表单

上传的文件是没有办法和表单内容一起异步的,可考虑使用jquery的ajaxfileupload,或是其他的插件,异步上传文件后,然后再对表单进行操作。

‘陆’ 怎么在上传文件的同时提交表单

可以用“风声无组件”上传,如果还想获取除了上传文件以外的其他提交信息,只要在上传类后面读取就可以了:
以下为检验页面代码:
<!--#include file="FSUpClass.asp"-->
'--上传类函数开始--
dim upload
set upload=New UpLoadClass
upload.MaxSize = 1048000
upload.FileType = "jpg/gif/png/bmp"
'上传文件存放目录
upload.SavePath = "Upfile/"
upload.open()

if upload.Error>0 then
response.write"<SCRIPT language=JavaScript>alert('上传图片只允许gif/jpg/png/bmp格式,且不能超过1MB。');"
response.write"javascript:history.go(-1)</SCRIPT>"
end if
'--上传类函数结束--

set rs=server.createobject("adodb.recordset")
sql="select * from Table where...."
rs.open sql,conn,1,3
rs.addnew
'Pic为你上传的图片的提交名
rs("Pic")=upload.form("Pic")
'text为你提交的文本信息
rs("text")=upload.form("text")
rs....
rs.update
rs.close

‘柒’ 关于AsyncHttpClient框架的post 提交表单上传文件怎么弄

用开源项目Asynchttpclient的GET_POST访问网络

* 使用GET方式提交数据:
//1、创建一个浏览器对象
AsyncHttpClient client = new AsyncHttpClient();
//2、发送一个GET请求
client.get(path, new AsyncHttpResponseHandler() {

/**
* 请求处理成功后调用这个方法
* statusCode 响应码 200 404 503
* headers 响应头信息
* responseBody 服务器返回的响应数据(如:登陆成功、失败等)
*/
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
Toast.makeText(MainActivity.this,
new String(responseBody), 0).show();
}

‘捌’ asp 提交表单和上传文件

asp.net和asp上传方式基本相似。都需要使用到form表单。下面分别介绍asp和asp.net两种文件上传方式。

第一种:asp方式

  1. 首先建立form表单

    <form name="form1" method="post" action="send.asp"enctype="multipart/form-data">

    <input name="title" type="text"/>

    <input name="uploadimg" type="file" />

    <input name="submit" type="submit" value="提交"/>

    </form>

    asp的表单一定要注意加上enctype="multipart/form-data"这个属性,否则是上传不了图片的,这个属性很关键。

  2. 上传处理代码也就是send.asp的处理代码。

    因为asp本身没有上传的组件或控件,这里只能借助第三方式的组件或类。上传组件推荐使用aspJpeg组件,这个组件不仅可以上传文件,如果是图片的话,可以调节尺寸尺寸,创建缩略图等。很方便,目前一般的空间商都支持这个组件,另外就是使用组件上传类,像风声,无惧等,都是无组件上传。

  3. 上传代码,这里假设采用的是风声无组件上传类。类文件已经包含进去。

    <include file="uploadclass.asp"-->

    处理代码:

    先初始化上传类

    dim myrequest

    set myrequest = new UpLoadClass

    设置属性:

    myrequest.FileType="gif,jpeg,jpg,png" //设置上传类型

myrequest.SavePath="../upload/" //设置上传路径

myrequest.MaxSize=100*1024 //设置上传文件的大小,

myrequest.AutoSave=1 //设置保存方式,为1表示自动保存

myrequest.Open //打开文件流


title =myrequest.form("title")

img =myrequest.form("uploadimg") //这两部表示接收form表单中的数据。不 能再用request.form或request.querystring来接收了,必须用刚才初始化的对象

myrequest来接收。

现在数据接收到了,剩下的就是保存到数据库。这和常用的数据保存方式一下的,拼接SQL.然后再执行,这里不赘述。


第二种asp.net方式。

asp.net方式,因为采用的是服务器控件,所以与asp有点区别。

  1. 首先是form不同,asp.net的form是服务器控件,需要添加ID和runat="Server"如

    <form name="form1" ID="form1" runat="Server">

    <asp:FileUpload ID="FileUpload1"runat="server" />
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" />

    </form>

  2. 处理代码,上传并保存代码

    protectedvoidButton1_Click(objectsender, EventArgs e)
    {
    StringsavePath = Server.MapPath("upload");
    if(FileUpload1.HasFile)
    {
    Stringfilename;
    filename = FileUpload1.FileName;
    savePath +=filename;
    FileUpload1.SaveAs(savePath);
    Page.Response.Write(FileUpload1.PostedFile.ContentType + FileUpload1.PostedFile.ContentLength+"<br>");
    Page.Response.Write("<img src='"+savePath+"'>");

    }
    else
    {
    Page.Response.Write("fff");
    }
    }

asp提交和上传文件,与asp.net是不同的。一个是html标签,一个是服务器控件。相对来说,asp.net的文件上传比asp简单很多,因为asp.net提供了相对应的上传控件。而asp没有。只有采用第三方组件或无组件上传类。上传文件成功后,另外还可以加入更多的处理元素,如图片入库,加上水印等,这需要你自己去思考和查找资料了。

‘玖’ 如何利用curl实现form表单提交 带文件上传

php">//上传D盘下的test.jpg文件,文件必须存在,否则curl处理失败且没有任何提示
$data=array('name'=>'Foo','file'=>'@d:/test.jpg');
注:PHP5.5.0起,文件上传建议使用CURLFile代替@

$ch=curl_init('http://localhost/upload.php');
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_exec($ch);

更多内容请参考:http://www.zjmainstay.cn/php-curl#十模拟上传文件

‘拾’ 怎么将文件上传和表单提交分开处理

上传这样的功能本来就应该独立出来,为以后能够更好的扩展和组件重用做准备。至于你的这个问题也很容易处理,问题的关键不在于后台处理的程序如何安排,而在于页面的表现形式,因为程序本身既然独立出来了,你的ACTION也是独立出来的,那么就把页面里的上传功能独立出来。
举例来说:
在你的表单里添加一个上传按钮,点击后弹出一个漂浮的div图层,然后在这个图层里可以放置单个的文件浏览按钮,或者动态的增加多个需要上传的浏览按钮,都是很灵活的,然后把这个图层使用单独的form标签包围起来就可以了。
类似的功能像很多论坛程序都在使用,比较成熟的js组件也可以实现,比如ExtJs、jQuery等等,一般来说能够提供web editor功能的js组件对于上传都是类似的处理方式。

热点内容
如何寻找资产配置机会 发布:2024-10-13 19:13:47 浏览:374
轿车安卓中控怎么安装手机卡 发布:2024-10-13 19:05:23 浏览:450
商城首页android 发布:2024-10-13 17:53:20 浏览:496
甲骨文云服务器如何申请 发布:2024-10-13 16:49:39 浏览:135
c语言中参数传递 发布:2024-10-13 16:30:15 浏览:82
cos服务器搭建 发布:2024-10-13 16:17:41 浏览:338
象棋软件算法 发布:2024-10-13 15:32:35 浏览:903
平板怎么看真正配置 发布:2024-10-13 14:53:32 浏览:35
微信存储空间的其他 发布:2024-10-13 14:52:14 浏览:672
怎么绕过系统密码登录密码登录密码登录 发布:2024-10-13 14:47:41 浏览:510