gpt4 book ai didi

c - 使用表单数据时看不到 C 中上传的文件

转载 作者:行者123 更新时间:2023-11-30 14:32:54 24 4
gpt4 key购买 nike

我有一个简单的 C 语言 Web 服务器,可以打印从客户端接收到的所有内容。它工作得很好,除了当我尝试使用 multipart/form-data 上传文件时,我尝试使用 Postman 和curl 来执行此操作,但结果相同。这可能是什么原因?

但是当我选择binary时,我会在输出中看到我上传的文件。

当使用表单数据时,我得到这个(我没有看到上传的文件):

POST /hello-world/ HTTP/1.1
Content-Type: multipart/form-data; boundary=--------------------------257071285279776422109124
User-Agent: PostmanRuntime/7.21.0
Host: localhost:8088
Accept-Encoding: gzip, deflate
Content-Length: 753
Connection: keep-alive

----------------------------257071285279776422109124
Content-Disposition: form-data; name="filecoming2"; filename="imgage.jpg"
Content-Type: image/jpeg

80 79 83 84 32 47 104 101 108 108 111 45 119 111 114 108 100 47 32 72 84 84 80 47 49 46 49 13 10 67 111 110 116 101 110 116 45 84 121 112 101 58 32 109 117 108 116 105 112 97 114 116 47 102 111 114 109 45 100 97 116 97 59 32 98 111 117 110 100 97 114 121 61 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 50 53 55 48 55 49 50 56 53 50 55 57 55 55 54 52 50 50 49 48 57 49 50 52 13 10 85 115 101 114 45 65 103 101 110 116 58 32 80 111 115 116 109 97 110 82 117 110 116 105 109 101 47 55 46 50 49 46 48 13 10 65 99 99 101 112 116 58 32 42 47 42 13 10 67 97 99 104 101 45 67 111 110 116 114 111 108 58 32 110 111 45 99 97 99 104 101 13 10 80 111 115 116 109 97 110 45 84 111 107 101 110 58 32 99 48 50 100 55 99 49 57 45 102 100 50 52 45 52 49 97 48 45 56 52 53 99 45 100 56 54 49 101 99 55 54 48 102 56 99 13 10 72 111 115 116 58 32 108 111 99 97 108 104 111 115 116 58 56 48 56 56 13 10 65 99 99 101 112 116 45 69 110 99 111 100 105 110 103 58 32 103 122 105 112 44 32 100 101 102 108 97 116 101 13 10 67 111 110 116 101 110 116 45 76 101 110 103 116 104 58 32 55 53 51 13 10 67 111 110 110 101 99 116 105 111 110 58 32 107 101 101 112 45 97 108 105 118 101 13 10 13 10 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 50 53 55 48 55 49 50 56 53 50 55 57 55 55 54 52 50 50 49 48 57 49 50 52 13 10 67 111 110 116 101 110 116 45 68 105 115 112 111 115 105 116 105 111 110 58 32 102 111 114 109 45 100 97 116 97 59 32 110 97 109 101 61 34 102 105 108 101 99 111 109 105 110 103 50 34 59 32 102 105 108 101 110 97 109 101 61 34 105 109 103 97 103 101 46 106 112 103 34 13 10 67 111 110 116 101 110 116 45 84 121 112 101 58 32 105 109 97 103 101 47 106 112 101 103 13 10 13 10

当使用二进制(在Postman中)时,我看到了我上传的文件!

POST /hello-world/ HTTP/1.1
Content-Type: application/x-www-form-urlencoded
User-Agent: PostmanRuntime/7.21.0
Host: localhost:8088
Accept-Encoding: gzip, deflate
Content-Length: 538
Connection: keep-alive

����

80 79 83 84 32 47 104 101 108 108 111 45 119 111 114 108 100 47 32 72 84 84 80 47 49 46 49 13 10 67 111 110 116 101 110 116 45 84 121 112 101 58 32 97 112 112 108 105 99 97 116 105 111 110 47 120 45 119 119 119 45 102 111 114 109 45 117 114 108 101 110 99 111 100 101 100 13 10 85 115 101 114 45 65 103 101 110 116 58 32 80 111 115 116 109 97 110 82 117 110 116 105 109 101 47 55 46 50 49 46 48 13 10 65 99 99 101 112 116 58 32 42 47 42 13 10 67 97 99 104 101 45 67 111 110 116 114 111 108 58 32 110 111 45 99 97 99 104 101 13 10 80 111 115 116 109 97 110 45 84 111 107 101 110 58 32 55 51 56 99 55 55 55 51 45 50 102 57 52 45 52 101 100 102 45 56 99 52 102 45 48 48 55 99 54 50 98 51 48 50 55 53 13 10 72 111 115 116 58 32 108 111 99 97 108 104 111 115 116 58 56 48 56 56 13 10 65 99 99 101 112 116 45 69 110 99 111 100 105 110 103 58 32 103 122 105 112 44 32 100 101 102 108 97 116 101 13 10 67 111 110 116 101 110 116 45 76 101 110 103 116 104 58 32 53 51 56 13 10 67 111 110 110 101 99 116 105 111 110 58 32 107 101 101 112 45 97 108 105 118 101 13 10 13 10 -1 -40 -1 -32 0 16 74 70 73 70 0 1 1 1 0 72 0 72 0 0 -1 -2 0 19 67 114 101 97 116 101 100 32 119 105 116 104 32 71 73 77 80 -1 -37 0 67 0 80 55 60 70 60 50 80 70 65 70 90 85 80 95 120 -56 -126 120 110 110 120 -11 -81 -71 -111 -56 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -37 0 67 1 85 90 90 120 105 120 -21 -126 -126 -21 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -62 0 17 8 0 1 0 1 3 1 17 0 2 17 1 3 17 1 -1 -60 0 20 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 -1 -60 0 20 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -38 0 12 3 1 0 2 16 3 16 0 0 1 3 -1 -60 0 20 16 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -38 0 8 1 1 0 1 5 2 127 -1 -60 0 20 17 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -38 0 8 1 3 1 1 63 1 127 -1 -60 0 20 17 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -38 0 8 1 2 1 1 63 1 127 -1 -60 0 20 16 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -38 0 8 1 1 0 6 63 2 127 -1 -60 0 20 16 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -38 0 8 1 1 0 1 63 33 127 -1 -38 0 12 3 1 0 2 0 3 0 0 0 16 -97 -1 -60 0 20 17 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -38 0 8 1 3 1 1 63 16 127 -1 -60 0 20 17 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -38 0 8 1 2 1 1 63 16 127 -1 -60 0 20 16 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -38 0 8 1 1 0 1 63 16 127 -1 -39

这是我的网络服务器,位于 C:

#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <stdbool.h>

#define PORT 8088

int main(void)
{
int server_fd, new_socket;
long valread;
struct sockaddr_in address;

int addrlen = sizeof(address);

char *hello = "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 12\n\nHello world!";

server_fd = socket(AF_INET, SOCK_STREAM, 0);

address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );

//memset(address.sin_zero, '\0', sizeof address.sin_zero);

bind(server_fd, (struct sockaddr *)&address, sizeof(address));

listen(server_fd, 10);

while(true)
{
printf("\n+++++++ Waiting for new connection ++++++++\n\n");

new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen);

char buffer[30000] = {0};

valread = read(new_socket, buffer, 30000);
printf("%s\n", buffer);

printf("\n");
for (int i = 0; i < valread; i++) { printf("%i ", buffer[i]); }
printf("\n\n");

write(new_socket, hello, strlen(hello));
printf("------------------Hello message sent-------------------\n");

close(new_socket);
}
}

最佳答案

TCP 是面向流的。您需要重复调​​用readwrite。考虑到您的示例,对 read 的单次调用并不能保证用整个文件填充缓冲区。读取 HTTP header ,解析 Content-Length 并继续读取,直到 valread 总和等于 header 大小加上 Content-Length。

关于c - 使用表单数据时看不到 C 中上传的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59632171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com