文件上传失败解决过程

写了一个html+cgi实现文件上传的功能,使用html始终无法将文件上传上去,cgi解析不到文件名和文件内容。使用postman就可以上传成功。下面是源文件:
post.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Test</title>
</head>

<body>
    <form action="/cgi-bin/out.cgi" method="POST">
        <input name="file" type="file" />
        <input type="submit" />
    </form>
</body>

</html>

out.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "cgic.h"
#define BufferLen 1024
int cgiMain(void)
{
	cgiFilePtr file;
	int targetFile;
	mode_t mode;
	char name[128];
	char fileNameOnServer[64];
	char contentType[1024];
	char buffer[BufferLen];
	char *tmpStr = NULL;
	int size;
	int got, t;
	cgiHeaderContentType("text/html");
	printf("aaa\n");
	//取得html页面中file元素的值,应该是文件在客户机上的路径名
	if (cgiFormFileName("file", name, sizeof(name)) != cgiFormSuccess)
	{
		fprintf(stdout, "could not retrieve filename\n");
		goto FAIL;
	}
	cgiFormFileSize("file", &size);
	//取得文件类型,不过本例中并未使用
	cgiFormFileContentType("file", contentType, sizeof(contentType));
	//目前文件存在于系统临时文件夹中,通常为/tmp,通过该命令打开临时文件。临时文件的名字与用户文件的名字不同,所以不能通过路径/tmp/userfilename的方式获得文件
	if (cgiFormFileOpen("file", &file) != cgiFormSuccess)
	{
		fprintf(stdout, "could not open the file\n");
		goto FAIL;
	}
	t = -1;
	//从路径名解析出用户文件名
	while (1)
	{
		tmpStr = strstr(name + t + 1, "\\");
		if (NULL == tmpStr)
			tmpStr = strstr(name + t + 1, "/"); //if "\" is not path separator, try "/"
		if (NULL != tmpStr)
			t = (int)(tmpStr - name);
		else
			break;
	}
	strcpy(fileNameOnServer, name + t + 1);
	printf("%s\n", fileNameOnServer);
	mode = S_IRWXU | S_IRGRP | S_IROTH;
	//在当前目录下建立新的文件,第一个参数实际上是路径名,此处的含义是在cgi程序所在的目录(当前目录))建立新文件
	targetFile = open(fileNameOnServer, O_RDWR | O_CREAT | O_TRUNC | O_APPEND, mode);
	if (targetFile < 0)
	{
		fprintf(stdout, "could not create the new file,%s\n", fileNameOnServer);
		goto FAIL;
	}
	//从系统临时文件中读出文件内容,并放到刚创建的目标文件中
	while (cgiFormFileRead(file, buffer, BufferLen, &got) == cgiFormSuccess)
	{
		if (got > 0)
			write(targetFile, buffer, got);
	}
	cgiFormFileClose(file);
	close(targetFile);
	goto END;
FAIL:
	printf("Failed to upload");
	return 1;
END:
	printf("File " "%s" " has been uploaded", fileNameOnServer);
	return 0;
}

因为用postman可以上传文件成功,所以定位问题在html文件中,但是html就只有三行关于form表单提交的代码,哪里会出错呢,会不会是文件编码有问题,加了 <meta charset="utf-8"> 还是不行,进行了若干次修改后还是失败。没办法,那就抓包找用html文件和用postman上传的差异:
postman:
在这里插入图片描述
用html文件上传:
在这里插入图片描述
发现两者的Content-Type不一样,
postman的为content-type: multipart/form-data;
用html文件上传的是:Content-Type: application/x-www-form-urlencoded
这是不是就是上传失败的原因呢?
在html文件中指定类型:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Test</title>
</head>

<body>
    <form action="/cgi-bin/out.cgi" method="POST" enctype="multipart/form-data">
        <input name="file" type="file" />
        <input type="submit" />
    </form>
</body>

</html>

上传成功!
over

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Li-Yongjun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值