github.com/kotovmak/go-admin@v1.1.1/template/types/action/file_upload.go (about)

     1  package action
     2  
     3  import (
     4  	"html/template"
     5  
     6  	"github.com/kotovmak/go-admin/context"
     7  	"github.com/kotovmak/go-admin/modules/constant"
     8  	"github.com/kotovmak/go-admin/template/types"
     9  )
    10  
    11  type FileUploadAction struct {
    12  	BaseAction
    13  	Url      string
    14  	Method   string
    15  	FileName string
    16  	Handlers []context.Handler
    17  }
    18  
    19  func FileUpload(id string, handler types.Handler) *FileUploadAction {
    20  	if id == "" {
    21  		panic("wrong file upload action parameter, empty id")
    22  	}
    23  	return &FileUploadAction{
    24  		Url:      URL(id),
    25  		Method:   "post",
    26  		FileName: "file",
    27  		Handlers: context.Handlers{handler.Wrap()},
    28  	}
    29  }
    30  
    31  func (file *FileUploadAction) SetUrl(url string) *FileUploadAction {
    32  	file.Url = url
    33  	return file
    34  }
    35  
    36  func (file *FileUploadAction) SetMethod(method string) *FileUploadAction {
    37  	file.Method = method
    38  	return file
    39  }
    40  
    41  func (file *FileUploadAction) GetCallbacks() context.Node {
    42  	return context.Node{
    43  		Path:     file.Url,
    44  		Method:   file.Method,
    45  		Handlers: file.Handlers,
    46  		Value:    map[string]interface{}{constant.ContextNodeNeedAuth: 1},
    47  	}
    48  }
    49  
    50  func (file *FileUploadAction) Js() template.JS {
    51  
    52  	return template.JS(`$('` + file.BtnId + `').on('click', function(){
    53    $('` + file.BtnId + `_input').click();
    54  });
    55  
    56  $("` + file.BtnId + `_input").on("change", function () {
    57    var files = $('` + file.BtnId + `_input').prop('files');
    58  
    59    var data = new FormData();
    60    data.append('` + file.FileName + `', files[0]);
    61    NProgress.start();
    62  
    63    $.ajax({
    64  		url: '` + file.Url + `',
    65  		type: '` + file.Method + `',
    66  		data: data,
    67  		cache: false,
    68    		processData: false,
    69    		contentType: false,
    70  		success: function (data) { 
    71  		  	NProgress.done();
    72  			if (data.code === 0) {
    73  				swal(data.msg, '', 'success');
    74  				$.pjax.reload('#pjax-container');
    75  			} else {
    76  				swal(data.msg, '', 'error');
    77  			}
    78  		},
    79  		error: function (data) {
    80  			NProgress.done();
    81  			if (data.responseText !== "") {
    82  				swal(data.responseJSON.msg, '', 'error');								
    83  			} else {
    84  				swal('error', '', 'error');
    85  			}
    86  		},
    87    });
    88  });`)
    89  }
    90  
    91  func (file *FileUploadAction) BtnAttribute() template.HTML {
    92  	return template.HTML(`href="javascript:;"`)
    93  }
    94  
    95  func (file *FileUploadAction) FooterContent() template.HTML {
    96  	return template.HTML(`<input class="` + file.BtnId[1:] + `_input" type="file" multiple="multiple" style="display:none" />`)
    97  
    98  }