我是新来的反应js.我想用react js异步上传图像假设我有这段代码
var FormBox = React.createClass({
getInitialState: function () {
return {
photo: []
}
},
pressButton: function () {
var data = new FormData();
data.append("photo", this.state.photo);
// is this the correct way to get file data?
},
getPhoto: function (e) {
this.setState({
photo: e.target.files[0]
})
},
render: function () {
return (
<form action='.' enctype="multipart/form-data">
<input type='file' onChange={this.getPhoto}/>
<button onClick={this.pressButton}> Get it </button>
</form>
)
}
})
ReactDOM.render(<FormBox />, document.getElementById('root'))
Run Code Online (Sandbox Code Playgroud)
任何答案将不胜感激!
我有新的反应,我很好奇如何正确地做到这一点.假设我有这个表单,点击按钮,我想得到文本框值,
var form = React.createClass({
submit : function(e){
//how to get textbox value?
},
render : function(){
return (
<form>
<input type="text" id="textbox" value="hey, get me"/>
<button onClick={this.submit}>Get it</button>
</form>
);
}
});
Run Code Online (Sandbox Code Playgroud)
任何答案将不胜感激!谢谢!
我是新来的。我试图将组件和动作功能分开。但是我无法从单独的动作函数中获取返回值。是否可以从调度函数返回值(例如,对象{})
我把简短的代码如下:
LoginComponent.js
class Login extends React.Component {
constructor(props){
super(props)
this.state = {
username : '',
password : ''
}
}
submit = (e) => {
/* console.logging "Some response"*/
console.log(this.props.doLogin(this.state))
}
render(){
return (
<form onSubmit={this.submit}>/* some login element */</form>
)
}
}
export default connect(null, {LoginAction})(Login);
Run Code Online (Sandbox Code Playgroud)
LoginAction.js
export function doLogin(state){
return dispatch => {
return axios.post('login', state).then(res =>{
return "Some response";
})
}
}
Run Code Online (Sandbox Code Playgroud)
但它不返回任何值
谢谢。
我想知道是否可以将动态结构作为函数的参数传递?
type ReturnValue struct {
Status string
CustomStruct // here should store any struct given
}
func GetReturn(status string, class interface{}){
var result = ReturnValue {Status : status, CustomStruct : //here any struct should be stored}
fmt.Prinln(result)
}
type Message1 struct {
message : string
}
func main(){
var message = Message1 {message: "Hello"}
GetReturn("success", message)
}
Run Code Online (Sandbox Code Playgroud) var person = {
"name" : "Bronn",
"description" : {
"occupation" : "guardian knight",
"age" : 52
}
}
for(var key in person){
if(person[key] == /*contains json object */){
//do something
}
}
Run Code Online (Sandbox Code Playgroud)
我想循环这个人并检查它的值是否包含单个数据或另一个对象。
import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
type User struct {
Name string `json:name`
Picture []uint8 `json:picture`
}
func main(){
//straight to the query
rows, err := 'SELECT name, picture FROM ms_users' // picture is longblob type in database
checkErr(err)
var usr User
for rows.Next(){
err = rows.Scan(&usr.Name, &usr.Picture)
checkErr(err)
}
jsn, err := json.Marshal(usr)
fmt.Printf("%v, "string(jsn))
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,我只得到 name 值但图片是空的。如何将 blob 值从数据库存储到 struct ?任何答案将不胜感激!谢谢你!
假设,我有以下表格
GUYS
| ID |
|:-----------|
| 1 |
| 2 |
| 3 |
Run Code Online (Sandbox Code Playgroud)
衬衫
| ID | GUYS_ID | color |
|:-----------|------------:|:------------:|
| 1 | 1 | red
| 2 | 1 | red
| 3 | 3 | red
| 4 | 3 | red
| 5 | 3 | red
Run Code Online (Sandbox Code Playgroud)
我使用此查询加入这些表
SELECT a.id, b.color
FROM GUYS a
LEFT JOIN SHIRT b
ON a.ID=b.GUYS_ID
WHERE b.color='red'
ORDER BY a.id ASC;
Run Code Online (Sandbox Code Playgroud)
这只获取其ID为ON SHIRT的GUYS
想知道是否有可能获取所有与SHIRT无关的GUYS?
实现这样的结果,
| ID | …Run Code Online (Sandbox Code Playgroud)