Ошибка 500 при загрузке нескольких частей httpwebrequest - PullRequest
0 голосов
/ 27 апреля 2020

Здравствуйте (извините за мой плохой английский sh), у меня проблема с моим Mantis / Starweb, когда я пытаюсь загрузить изображение jpg с httpwebrequest. Я думаю, что проблема в кодировке моего файла (потому что я могу легко добавить примечание к своему богомолу с моим кодом c#, но не загружать файл).

Вот код моей страницы php:

<form method="post" enctype="multipart/form-data" action="http://*******.fr/Starweb/bug_file_add.php">

  <input type="hidden" name="bug_id" value="id de la SW" />

  <input type="hidden" name="max_file_size" value="30000000" />

  <input name="file" type="file" size="40" />

  <input type="submit" value="Send the file" />

</form>

Вот мой код:

private void UploadMultipart(string numberMantisTicket, byte[] fileBytes, string filename, string filePath)
        {

            string urlMantisUploadFile = "http://****.fr/Mantis/bug_file_add.php";

            // Création du header et du footer
            string boundary = System.Guid.NewGuid().ToString();
            String header = string.Format("--{0}", boundary);
            string footer = header + "--";

            StringBuilder postData = new StringBuilder();
            // Paramètre bug_id = number of mantis ticket
            postData.AppendLine(header);
            postData.AppendLine("Content-Disposition: form-data; name =\"bug_id\"");
            postData.AppendLine();
            postData.AppendLine(numberMantisTicket);

            // Paramètre max_size_file défini par défaut.
            postData.AppendLine(header);
            postData.AppendLine("Content-Disposition: form-data; name =\"max_file_size\"");
            postData.AppendLine();
            postData.AppendLine("30000000");

            // Add file
            postData.AppendLine(header);
            postData.AppendLine(string.Format("Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"", filename));
            postData.AppendLine("Content-Type: image/jpeg");
            postData.AppendLine();
           postData.AppendLine(Encoding.UTF8.GetBytes(filePath + "\\" + fileName));
            postData.AppendLine(footer);

            byte[] postDataBytes = Encoding.UTF8.GetBytes(postData.ToString());

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(urlMantisUploadFile);
            request.PreAuthenticate = true;
            request.AllowWriteStreamBuffering = true;
            request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
            request.Method = "POST";
            request.ContentLength = postDataBytes.Length;


            try
            {
                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(postDataBytes, 0, postDataBytes.Length);
                    requestStream.Close();

                    // Uniquement pour les tests, sert à afficher ce que le serveur renvoi
                    using (WebResponse response = request.GetResponse())
                    {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            Logger.LogError(reader.ReadToEnd());
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.LogError("UploadMultipart " + e);
                throw e;
            }
        }

Вот мое сообщение об ошибке: Ошибка загрузки файла. Вероятно, это связано с тем, что размер файла был больше, чем в настоящее время разрешено этой установкой PHP.

Размер моего изображения: 30 килобайт ... (очень маленькая картинка)

Большое спасибо, если Вы можете помочь мне ...

Сердечно.

...