小编DDa*_*s25的帖子

如何在 SwiftUI 中显示来自 url 的图像

所以我正在尝试使用从我的 Node JS 服务器获取的数据创建一个内容提要。

在这里,我从我的 API 中获取数据

class Webservice {
    func getAllPosts(completion: @escaping ([Post]) -> ()) {
        guard let url = URL(string: "http://localhost:8000/albums")
     else {
     fatalError("URL is not correct!")
    }

        URLSession.shared.dataTask(with: url) { data, _, _ in

            let posts = try!

                JSONDecoder().decode([Post].self, from: data!); DispatchQueue.main.async {
                    completion(posts)
            }
        }.resume()
    }
}
Run Code Online (Sandbox Code Playgroud)

将变量设置为从 API 获取的数据

final class PostListViewModel: ObservableObject {

    init() {
        fetchPosts()
    }

    @Published var posts = [Post]()

    private func fetchPosts() {
        Webservice().getAllPosts {
            self.posts = $0
        }
    }


} …
Run Code Online (Sandbox Code Playgroud)

swift swiftui

31
推荐指数
8
解决办法
3万
查看次数

在将图像保存到服务器之前如何使用 event.target.files 填充 img src

我将图像保存到文件对象,然后使用该对象将其保存到服务器。

(ReactJS)

onChange(event) {
    this.setState({
      file: event.target.files[0],
      loaded: 0,
    });
  }

onClickHandler = () => {
    const { match: { params} } = this.props;
    const data = new FormData()
    console.log(this.state.file)
    data.append('file', this.state.file)
    axios.post(`http://localhost:3000/albums/${params.albumId}/upload`, data, {

    }).then(res => {
      console.log(res.statusText)
    })
  }
Run Code Online (Sandbox Code Playgroud)

(NodeJS - image-upload.js)

const getImageByAlbumId = (request, response) => {
  const { id }  = request.params;

  db.pool.query('SELECT * FROM file WHERE album_id = $1 ORDER BY album_id ASC', [id], (error, results) => {
    if (error) {
      throw error
    } …
Run Code Online (Sandbox Code Playgroud)

javascript postgresql node.js reactjs

5
推荐指数
1
解决办法
7017
查看次数

标签 统计

javascript ×1

node.js ×1

postgresql ×1

reactjs ×1

swift ×1

swiftui ×1