1***@qq.com
1***@qq.com
  • 发布:2022-12-19 08:54
  • 更新:2022-12-27 10:41
  • 阅读:170

按官方的例子代码, 来上传文件. 服务端该如何解析出这个文件?

分类:HBuilderX

上传图片, 发现首部和尾部, 都有加一些附加信息进来

------WebKitFormBoundary6a7XEkgemCOp1yuE
Content-Disposition: form-data; name="data"; filename="a.bmp"
Content-Type: image/bmp

BM6悀 6 ( € 塞 j$
j$
.....中间一堆是BMP的图片的数据.....
刃?刃?
------WebKitFormBoundary6a7XEkgemCOp1yuE--

服务端, 我该如何才能定位抓取到中间图片的信息呢?
恳请大家指点一下, 非常感谢

<template>  
    <view>  
        <!-- <page-head :title="title"></page-head> -->  
        <view class="uni-padding-wrap uni-common-mt">  
            <view class="demo">  
                <block v-if="imageSrc">  
                    <image :src="imageSrc" class="image" mode="widthFix"></image>  
                </block>  
                <block v-else>  
                    <view class="uni-hello-addfile" @click="chooseImage">+ 选择图片</view>  
                </block>  
            </view>  
        </view>  
    </view>  
</template>  
<script>  
    export default {  
        data() {  
            return {  
                title: 'uploadFile',  
                imageSrc: ''  
            }  
        },  
        onUnload() {  
            this.imageSrc = '';  
        },  
        methods: {  
            chooseImage: function() {  
                uni.chooseImage({  
                    count: 2,  
                    sizeType: ['compressed'],  
                    sourceType: ['album'],  
                    success: (res) => {  
                        console.log('chooseImage success, temp path is', res.tempFilePaths[0])  
                        var imageSrc = res.tempFilePaths[0]  
                        uni.uploadFile({  
                            //url: 'https://unidemo.dcloud.net.cn/upload',  
                            url: 'http://192.168.1.6:2688',  
                            filePath: imageSrc,  
                            //fileType: 'image',  
                            name: 'data',  
                            success: (res) => {  
                                console.log('uploadImage success, res is:', res)  
                                uni.showToast({  
                                    title: '上传成功',  
                                    icon: 'success',  
                                    duration: 1000  
                                })  
                                this.imageSrc = imageSrc  
                            },  
                            fail: (err) => {  
                                console.log('uploadImage fail', err);  
                                uni.showModal({  
                                    content: err.errMsg,  
                                    showCancel: false  
                                });  
                            }  
                        });  
                    },  
                    fail: (err) => {  
                        console.log('chooseImage fail', err)  
                        // #ifdef MP  
                        uni.getSetting({  
                            success: (res) => {  
                                let authStatus = res.authSetting['scope.album'];  
                                if (!authStatus) {  
                                    uni.showModal({  
                                        title: '授权失败',  
                                        content: 'Hello uni-app需要从您的相册获取图片,请在设置界面打开相关权限',  
                                        success: (res) => {  
                                            if (res.confirm) {  
                                                uni.openSetting()  
                                            }  
                                        }  
                                    })  
                                }  
                            }  
                        })  
                        // #endif  
                    }  
                })  
            }  
        }  
    }  
</script>  

<style>  
    .image {  
        width: 100%;  
    }  

    .demo {  
        background: #FFF;  
        padding: 50rpx;  
    }  
</style>  
2022-12-19 08:54 负责人:无 分享
已邀请:
呆狗的一生

呆狗的一生 - 呆狗的一生

什么意思,是服务端没有收到图片吗?

呆狗的一生

呆狗的一生 - 呆狗的一生

如果是nodejs服务器,解析图片可以用multer的npm包。其它语言我不会。

1***@qq.com

1***@qq.com (作者)

好了,我找到了, 我是用DELPHI的INDY HTTP SERVER

代码如下:


  I := 0;  

  If ARequestInfo.PostStream <> nil then  
  begin  
    ARequestInfo.PostStream.Position := 0;  
    msgEnd := False;  

    boundary := ExtractHeaderSubItem(ARequestInfo.ContentType, 'boundary',QuoteHTTP);  
    startboundary := '--' + boundary;  
    repeat  
      tmp := ReadLnFromStream(ARequestInfo.PostStream, -1, True);  
    until tmp = startboundary;  

    Decoder := TIdMessageDecoderMIME.Create(nil);  
    try  
      TIdMessageDecoderMIME(Decoder).MIMEBoundary := boundary;  

      tsValues := TStringList.Create;  
      try  
        repeat  
          Decoder.SourceStream := ARequestInfo.PostStream;  
          Decoder.FreeSourceStream := False;  
          Decoder.ReadHeader;  
          Inc(I);  
          case Decoder.PartType of  
          mcptAttachment, mcptText:  
          begin  
          ms := TMemoryStream.Create;  
          try  
          ms.Position := 0;  
          newdecoder := Decoder.ReadBody(ms, msgEnd);  
          tmp := Decoder.Headers.Text;  
          fname := Decoder.Filename;  //********** here  
          Decoder.Free;  
          Decoder := newdecoder;  
          if Decoder <> nil then  
          TIdMessageDecoderMIME(Decoder).MIMEBoundary := boundary;  

          if fname <> '' then  
          begin  
          ms.SaveToFile(fname);  
          end  
          else  
          begin  
          ms.SaveToFile(IntToStr(I) + '.txt');  
          end;  
          finally  
          ms.Free;  
          end;  
          end;  
          mcptIgnore:  
          Begin  
          FreeAndNil(Decoder);  
          Decoder := TIdMessageDecoderMIME.Create(nil);  
          TIdMessageDecoderMIME(Decoder).MIMEBoundary := boundary;  
          End;  
          mcptEOF:  
          begin  
          FreeAndNil(Decoder);  
          msgEnd := True  
          end;  
          end;  

        until (Decoder = nil) or (msgEnd);  
      finally  
        tsValues.Free;  
      end;  
    finally  
      FreeAndNil(Decoder);  
    end;  
  end;

要回复问题请先登录注册