English 中文(简体)
通过有字段的 http 窗体上传图像
原标题:Uploading image through http form with fields
  • 时间:2012-05-23 15:10:53
  •  标签:
  • c#
  • html

今天我有一个程序可以张贴到我的网站, 但我需要将文件上传支持添加到我的程序( 用于jpg ) 。 我有一个这样的问题, 只是让我的程序发布, 而现在经过许多小时后, 我无法让它与文件上传一起工作 。

Today my code is not very good but it works and I m the only one using it. Here is my code, abit stripped but I hope you get the idea what I need help with. 谢谢!

(“ 更新文件” 是图像的输入字段名称)

public static string Post(string url, int id, string message)
{
    try
    {
        string param =
            string.Format(
                "MAX_FILE_SIZE=2097152&id={0}&com={1}&upfile", id, message);

        byte[] bytes = Encoding.ASCII.GetBytes(param);

        var post = (HttpWebRequest)WebRequest.Create(url);

        post.ProtocolVersion = HttpVersion.Version10;
        post.Method = "POST";
        post.AllowAutoRedirect = false;
        post.ContentType = "application/x-www-form-urlencoded";
        post.ContentLength = bytes.Length;

        post.UserAgent =
            "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.15) Gecko/2009101601 Firefox/3.0.15 (.NET CLR 3.5.30729)";
        post.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

        Stream requestStream = post.GetRequestStream();
        requestStream.Write(bytes, 0, bytes.Length);
        requestStream.Close();

        var webResponse = (HttpWebResponse)post.GetResponse();
        var sr = new StreamReader(webResponse.GetResponseStream());
        return sr.ReadToEnd();
    }
    catch (Exception exception)
    {
        return null;
    }
}

Edit: Sorry for my poor explanation here is what I need help with: I use the method above today to post on my site, but I need add support to upload files in my method. I can t change how the webpage works in the close future so it have to be done by using a POST request. I can t get it to work then I try to do it myself, so I m wondering if anybody could help me with what need to be added to read a image and post it with the request!

谢谢!

最佳回答

您需要使用多部分/ form- data 的编码, 而不是应用程序/ x- www- form- urlencoded, 然后您需要相应编码日志数据

>HTTPWebrequest(mulpart/form-data)的上载文件

问题回答

I m assuming you need a better solution than your current one. If you are on c#, there is a FILE upload control that you can use to upload files. You can even restrict it to the types of valid files that you allow.

控件为文件上载

Shouldnt your Content type be multipart/form-data; ? If it is possible for you to use a upload class I can make one available to you.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签