@@ -24,10 +24,14 @@ package elton
2424
2525import (
2626 "errors"
27+ "fmt"
2728 "io"
29+ "mime"
2830 "mime/multipart"
31+ "net/textproto"
2932 "os"
3033 "path/filepath"
34+ "strings"
3135)
3236
3337type multipartForm struct {
@@ -65,6 +69,12 @@ func (f *multipartForm) AddField(name, value string) error {
6569 return f .w .WriteField (name , value )
6670}
6771
72+ var quoteEscaper = strings .NewReplacer ("\\ " , "\\ \\ " , `"` , "\\ \" " )
73+
74+ func escapeQuotes (s string ) string {
75+ return quoteEscaper .Replace (s )
76+ }
77+
6878// AddFile add a file to form, if the reader is nil, the filename will be used to open as reader
6979func (f * multipartForm ) AddFile (name , filename string , reader ... io.Reader ) error {
7080 err := f .newFileBuffer ()
@@ -86,8 +96,17 @@ func (f *multipartForm) AddFile(name, filename string, reader ...io.Reader) erro
8696 }()
8797 r = file
8898 }
89-
90- fw , err := f .w .CreateFormFile (name , filename )
99+ h := make (textproto.MIMEHeader )
100+ h .Set ("Content-Disposition" ,
101+ fmt .Sprintf (`form-data; name="%s"; filename="%s"` ,
102+ escapeQuotes (name ), escapeQuotes (filename )))
103+ ext := filepath .Ext (filename )
104+ contentType := mime .TypeByExtension (ext )
105+ if contentType == "" {
106+ contentType = "application/octet-stream"
107+ }
108+ h .Set ("Content-Type" , contentType )
109+ fw , err := f .w .CreatePart (h )
91110 if err != nil {
92111 return err
93112 }
0 commit comments