欢迎光临,了解微信小程序开发,就上易用通!

面向新手《十四》template的使用,上传文件uploadfile

发布:2018-02-01 17:49浏览: 来源:网络 作者:cola

一:template的使用

模板WXML提供模板(Template),可以在模板中定义代码片段,然后在不同的地方使用。可以保证格式以及数据的相同。
1-定义模板使用`<template name="tempName"></template>`标签定义模板,并将模板名称命名为tempName,赋值给属性name。在标签内部,定义模板结构。如下:
<!-- template.wxml -->
<!-- 

    index: int

    msg: string

    time: string

-->

<template name="msgItem">

    <view>

    <text> {{index}}: {{msg}} </text>

    <text> Time: {{time}} </text>

  </view>

</template>
2-使用模板使用<template is="tempName" />标签,在需要使用模板的地方。如果要用到js文件中的数据,则需要添加data属性。如下:
<!-- template.wxml -->
<template is="msgItem" data="{{...item}}"/>

<template is="msgItem" data="{{...item}}"/>

<template is="msgItem" data="{{...item}}"/>
此时在页面上就会重复显示三次相同的信息。
data中的数据,来源于js文件,如下:
<!-- template.js -->
Page({

  data: {

    item: {

      index: 0,

      msg: 'this is a template',

      time: '2016-09-15'

    }

  }

})
3-is属性is属性不仅可以静态的指向渲染的模板,也可以使用Mustache语法,来动态决定具体需要渲染哪个模板。如下:
<!-- template.wxml -->
// templates

<template name="odd">

    <view> odd </view>

</template>

<template name="odd">

    <view> even </view>

</template>


// is属性使用Mustache语法动态渲染template

<block wx:for="{{[1, 2, 3, 4,5]}}">

    <template is="{{item % 2 == 0 ? 'even' : 'odd'}}" />

</block>
如上代码,则会在页面中根据条件来显示odd 或是 even
4-模板的引用如上都是在同一个wxml文件中定义和引用模板,而模板的定义和引用是可以分开的。即在一个文件中定义模板,而在其他一个或多个文件wxml文件中都可以使用定义好的模板。
    从外部文件中引用模板,使用import src="templateUrl" />标签。同样使用is属性来指向要引用的标签。
    如目录如下:
-pages
    |--index

        |--index.js

        |--index.json

        |--index.wxml

        |--index.wxss

    |--template

        |--template.js

        |--template.json

        |--template.wxml

        |--template.wxss
要在index.wxml中使用template中定义的模板,则需要先在index中利用import来导入该模板:
<!-- index.wxml -->
<import src="../template/template.wxml"

<template is="msgItem" data={{...indexData}}
// 使用的是自己js文件中的数据
要注意import作用域,其仅仅引用目标文件中template。如:C import B,B import A,在C中可以使用B定义的template,在B中可以使用A定义的template,但是C不能使用A定义的template。
参考微信小程序文档-框架-视图层-WXML-模板
微信小程序文档-框架-视图层-WXML-引用


二:上传文件

wx.chooseImage({
      count: 1, // 默认9

      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有

      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有

      success: function (res) {

        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片

        var tempFilePaths = res.tempFilePaths

        console.log(tempFilePaths[0])

        wx.uploadFile({

      url: 'https://gh.ydfos.com/wxfileUplad/Post', //仅为示例,非真实的接口地址

      filePath: tempFilePaths[0],

      name: 'file',

      formData:{

        'address': 'jinhua',

        'name':'zjh'

      },

      success: function(res2){

        console.log(res2);

        var data = res2.data

        console.log(data);

        //do something

      }

    })

  }

      }

    )
服务端代码:
var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "config/log4net.config");
            XmlConfigurator.ConfigureAndWatch(logCfg);

            var logger = LogManager.GetLogger("log");

 

            try

            {

 

                logger.Info("files:" + Request.Files.Count);

                logger.Info("filename:" + Request.Files[0].FileName);

 

                 

                string filepath = Server.MapPath("/upload/");

                Stream sin = Request.InputStream;

 

                string name = Request.QueryString["name"];

                string fileExt = Path.GetExtension(Request.Files[0].FileName).ToLower();

                string fileName = Guid.NewGuid().ToString() + fileExt;

                Request.Files[0].SaveAs(filepath + fileName);

 

 

 

                logger.Info("name:" + Request.Form["name"] + ";address:" + Request.Form["address"]);

 

                return Json(new { success="ok"}, JsonRequestBehavior.AllowGet);

            }

            catch (Exception ex)

            {

                logger.Error(ex.Message);

                return Json(new { success = "fail" }, JsonRequestBehavior.AllowGet);

            }





免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。