C # winform interfaz cargar archivos adjuntos al servidor (springboot)

Interfaz de escritorio

Interfaz de referencia

Inserte la descripción de la imagen aquí

Código de referencia del botón Examinar:

private void btnBrower_Click(object sender, EventArgs e)
        {
    
    
            //创建文件弹出选择窗口(包括文件名)对象
            OpenFileDialog ofd = new OpenFileDialog();
            //判断选择的路径
            if (ofd.ShowDialog() == DialogResult.OK)
            {
    
    
                this.txtFilePath.Text = ofd.FileName.ToString();
            }
            filePath = this.txtFilePath.Text;
        }

Cargar el código de referencia del archivo:

string filePath = "";
private void btnUpload_Click(object sender, EventArgs e)
        {
    
    
            string address = "http://localhost:8080";
            string[] files = {
    
     filePath };
            uploadFiles(address, files);
        }
public static string uploadFiles(string url, string[] files)
        {
    
    

            Console.WriteLine(files.Length);

            // 设置参数
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            CookieContainer cookieContainer = new CookieContainer();
            request.CookieContainer = cookieContainer;
            request.AllowAutoRedirect = true;
            request.Method = "POST";
            string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
            request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
            const string filePartHeaderTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
                      "Content-Type: application/octet-stream\r\n\r\n";
            //string boundary = "kenny";          // 边界符
            byte[] beginBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");  // 边界符开始。【☆】右侧必须要有 \r\n 。
            byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); // 边界符结束。【☆】两侧必须要有 --\r\n 。
            byte[] newLineBytes = Encoding.UTF8.GetBytes("\r\n"); //换一行
            MemoryStream memoryStream = new MemoryStream();
            List<string> lstFiles = new List<string>();
            foreach (string fileFullName in files)
            {
    
    
                if (File.Exists(fileFullName))
                {
    
    
                    lstFiles.Add(fileFullName);
                }
            }

            int i = 0;
            foreach (var fileFullName in lstFiles)
            {
    
    
                FileInfo fileInfo = new FileInfo(fileFullName);
                string fileName = fileInfo.Name;
                fileName = DateTime.Now.ToString("yyyyMMddHHmmss") +"-"+ fileName;

                string fileHeaderItem = string.Format(filePartHeaderTemplate, "file", fileName);
                byte[] fileHeaderItemBytes = Encoding.UTF8.GetBytes(fileHeaderItem);

                if (i > 0)
                {
    
    
                    // 第一笔及第一笔之后的数据项之间要增加一个换行 
                    memoryStream.Write(newLineBytes, 0, newLineBytes.Length);
                }
                memoryStream.Write(beginBoundaryBytes, 0, beginBoundaryBytes.Length);  // 2.1 写入FormData项的开始边界符
                memoryStream.Write(fileHeaderItemBytes, 0, fileHeaderItemBytes.Length); // 2.2 将文件头写入FormData项中

                int bytesRead;
                byte[] buffer = new byte[1024];

                FileStream fileStream = new FileStream(fileFullName, FileMode.Open, FileAccess.Read);
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                {
    
    
                    memoryStream.Write(buffer, 0, bytesRead);        // 2.3 将文件流写入FormData项中
                }
                i++;
            }

            memoryStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);    // 2.4 写入FormData的结束边界符

            request.ContentLength = memoryStream.Length;
            Stream requestStream = request.GetRequestStream();
            memoryStream.Position = 0;
            byte[] tempBuffer = new byte[memoryStream.Length];
            memoryStream.Read(tempBuffer, 0, tempBuffer.Length);
            memoryStream.Close();
            requestStream.Write(tempBuffer, 0, tempBuffer.Length);  // 将内存流中的字节写入 httpWebRequest 的请求流中
            requestStream.Close();

            //Console.WriteLine("111");
            //发送请求并获取相应回应数据
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            //直到request.GetResponse()程序才开始向目标网页发送Post请求
            Stream instream = response.GetResponseStream();
            StreamReader sr = new StreamReader(instream, Encoding.UTF8);
            //返回结果网页(html)代码
            string content = sr.ReadToEnd();
            string statusCode = response.StatusCode.ToString();
            return statusCode;
        }

extremo posterior

El ejemplo oficial de usar springboot
ver:
https://spring.io/guides/gs/uploading-files/

https://github.com/spring-guides/gs-uploading-files

Adjunto:

Para referencia:
c # cargue archivos en lotes, use springboot MultipartFile para recibir en segundo plano:
https://blog.csdn.net/yongjiutongmi53151/article/details/100160994

Los extremos frontal y posterior están todos en C #, consulte:
cliente (formulario Winform) carga de archivos al servidor (formulario web) Ejemplo simple:
https://www.cnblogs.com/xielong/p/5711000.html

Supongo que te gusta

Origin blog.csdn.net/s_156/article/details/110951617
Recomendado
Clasificación