如何在同一帖子中上传数据和文件?(How can I upload data and a file in the same post?)

编程入门 行业动态 更新时间:2024-10-28 11:30:06
如何在同一帖子中上传数据和文件?(How can I upload data and a file in the same post?)

我需要将pdf文件和电话号码上传到将发送传真的服务。

有效的表单(来自网页)如下所示:

<form action="send.php" method="post" enctype="multipart/form-data"> <input type="file" name="pdf" id="pdf" /> <input type="text" name="phonenumber" id="phonenumber" /> <input type="submit" name="Submit" /> </form>

问题是我需要从用C#编写的Windows应用程序中完成。

如何在同一篇文章中上传文件和字符串?

我正在使用WebClient类。 我尝试打开文件,读取其字节,并发布如下所示:

string content = "phonenumber="+request.PhoneNumber+"&pdf="; WebClient c = new WebClient(); c.Headers.Add("Content-Type", "multipart/form-data"); c.Headers.Add("Cache-Control", "no-cache"); c.Headers.Add("Pragma", "no-cache"); byte[] bret = null; byte[] p1 = Encoding.ASCII.GetBytes(content); byte[] p2 = null; using (StreamReader sr = new StreamReader(request.PdfPath)) { using (BinaryReader br = new BinaryReader(sr.BaseStream)) { p2 = br.ReadBytes((int)sr.BaseStream.Length); } } byte[] all = new byte[p1.Length + p2.Length]; Array.Copy(p1, 0, all, 0, p1.Length); Array.Copy(p2, 0, all, p1.Length, p2.Length); bret = c.UploadData(url, "POST", all);

这不起作用。

我没有服务器日志或类似的东西来帮助我调试它。

我是否在WebClient类中遗漏了一些简单的内容? 是否还有其他方法可以将UploadFile和UploadData结合起来发布两个值,如网页(有效)吗?

I need to upload a pdf file and a phone number to a service that will send a fax.

The form that works (from a webpage) looks like this:

<form action="send.php" method="post" enctype="multipart/form-data"> <input type="file" name="pdf" id="pdf" /> <input type="text" name="phonenumber" id="phonenumber" /> <input type="submit" name="Submit" /> </form>

The problem is that I need to do it from a windows application written in C#.

How can I upload both a file and a string in the same post?

I am using the WebClient class. I tried opening the file, reading its bytes, and posting everything like this:

string content = "phonenumber="+request.PhoneNumber+"&pdf="; WebClient c = new WebClient(); c.Headers.Add("Content-Type", "multipart/form-data"); c.Headers.Add("Cache-Control", "no-cache"); c.Headers.Add("Pragma", "no-cache"); byte[] bret = null; byte[] p1 = Encoding.ASCII.GetBytes(content); byte[] p2 = null; using (StreamReader sr = new StreamReader(request.PdfPath)) { using (BinaryReader br = new BinaryReader(sr.BaseStream)) { p2 = br.ReadBytes((int)sr.BaseStream.Length); } } byte[] all = new byte[p1.Length + p2.Length]; Array.Copy(p1, 0, all, 0, p1.Length); Array.Copy(p2, 0, all, p1.Length, p2.Length); bret = c.UploadData(url, "POST", all);

This does not work.

I do not have server logs or anything like that to help me debug it.

Am I missing something simple from the WebClient class? Is there any other way to combine UploadFile and UploadData to post both values like the webpage (that works) does?

最满意答案

首先,在multipart / form-data标题中添加c.Headers.Add时会出现拼写错误。 :-)

其次,您需要通过在内容部分之间引入边界来正确格式化帖子。 看看这里 。

First of all, you have a typo when doing c.Headers.Add in the multipart/form-data header. :-)

Second, you need to format your post correctly by introducing boundaries between the content parts. Take a look here.

更多推荐

本文发布于:2023-08-06 15:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1450995.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传   文件   帖子   数据   在同一

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!