CGI实现文件的上传和下载

2 篇文章 0 订阅

1.文件上传

       该功能通过使用cgic来实现。

(1)界面代码

<form action="file-up/download.cgi" enctype="multipart/form-data" method="post"> 
    <table>                             // 上传文件必须设置为post + multipart/form-data
	<tbody>
	<tr>
	    <td> 上传配置文件 </td>
	    <td><input type="file" name="uploadfile" value=""></td>
	    <td><input type="submit" name="upload" value="导入"></td>
	</tr>
	
	<tr>
	    <td> 下载配置文件 </td>
	    <td colspan="2"><a href="/download.html"> 配置文件导出地址 </a></td>
	</tr>
	</tbody>
    </table>
    <input type="submit" name="restart" value="刷新" >
</form>

(2)执行函数代码

enum ErrLog
{
    ErrSucceed,
    ErrOpenFile,
    ErrNoFile
};
enum ErrLog UploadFile()
{
    cgiFilePtr file;
    FILE *fd;
    char name[512];
    char path[128];
    char contentType[1024];
    int size = 0;
    int got = 0;
    int t = 0;
    char *tmp = NULL;

    if (cgiFormFileName("uploadfile", name, sizeof(name)) != cgiFormSuccess) //获取客户端pathname 
    {
        printf("<p> No file was uploaded. </p>\n");
        return ErrNoFile;
    }
    fprintf(cgiOut, "The filename submitted was: ");
    cgiHtmlEscape(name);
    fprintf(cgiOut, "<br>\n");
	
    cgiFormFileSize("uploadfile", &size);
    fprintf(cgiOut, "The file size was: %d bytes<br>\n", size);
	
    cgiFormFileContentType("uploadfile", contentType, sizeof(contentType));
    fprintf(cgiOut, "The alleged content type of the file was: ");
    cgiHtmlEscape(contentType);
    fprintf(cgiOut, "<br>\n");

    if (cgiFormFileOpen("uploadfile", &file) != cgiFormSuccess)  //尝试打开上传的,并存放在系统中的临时文件
    {
        fprintf(cgiOut, "<p> Could not open the file. </p>\n");
        return ErrOpenFile;
    }

    t = -1;
    while (1)
    {
        tmp = strstr(name+t+1, "\\");  // 从pathname解析出filename
        if (NULL == tmp)
        {
            tmp = strstr(name+t+1, "/");
        }
        if (NULL != tmp)
        {
            t = (int)(tmp-name);
        }
        else
        {
            break;
        }
    }
    tmp = (char *)malloc(size * sizeof(char)); // 在底层建立新文件
    strcpy(path, "/usr/local/boa/data/");
    strcat(path, name+t+1);  
    fd = fopen(path, "w+");
    if (fd == NULL)
    {
        return ErrOpenFile;
    }

    while (cgiFormFileRead(file, tmp, size, &got) == cgiFormSuccess) // 从临时文件读出content
    {
        fwrite(tmp, size, sizeof(char), fd);  //把读出的content写入新文件
    }
    fprintf(cgiOut, "<p> Upload File Success. </p>\n");

    cgiFormFileClose(file);
    free(tmp);
    fclose(fd);
    return ErrSucceed;
}

2.文件下载

(1)借助HTML中a标签实现,如:<a href="filepath" download="filename"> download </a>,则打开浏览器点击链接即可实现文件下载。

注:其中download属性可避免直接打开文件,进而执行下载任务;该方式的优点是在静态html界面即可实现文件下载,缺点是暴露了文件及其路径。

界面代码

<body>		
    <table>
    <tr>
    	<td><a href="/data/xx.cfg" download="xx.cfg"> xx设置 </a></td>
    </tr>

    <tr>
    	<td><a href="/data/xx.cfg" download="xx.cfg"> xx模块 </a></td>
    </tr>
    </table>

    <input type="button" value="返回" onclick="location.href='/cgi-bin/file-up/download.cgi'">
</body>

(2)当然,上述操作借助了部分浏览器内嵌的功能,而对于另一部分浏览器来说必须通过后台代码来实现文件的读、写和存,间接实现文件的下载,该部分可参考《嵌入式Linux下基于CGI的文件上传下载的实现》。

执行代码

void download(char *filename)
{
    FIFE *fp;
    char buff[SIZE];
    struct stat s;
    time_t date;
    int n;

    date = time(NULL);
    stat(filename, &s);
    printf(“Content Disposition:filename=\“%s\” date=%s\n”, filename, ctime(&date));
    printf(“Content Length:size==%d\n”, s.st_size);
    if (fp = fopen(filename, “r”))
    {
        while ((n = fread(buff, sizeof(char), sizeof(buff), fp)) > 0)
        {
            fwrite(buff, n, 1, stdout)
        }
        fclose(fp);
    }
}

 

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值