gpt4 book ai didi

node.js - 如何从 Angular 发送https post请求?

转载 作者:可可西里 更新时间:2023-11-01 10:45:01 27 4
gpt4 key购买 nike

当我尝试在 heroku 上上传图片时出现此错误:混合内容:位于“https://twassol.herokuapp.com/”的页面' 已通过 HTTPS 加载,但请求了不安全的图像 ' http://twassol.herokuapp.com/images/%D8%A8%D8%B7%D8%A7%D8%B1%D9%8A%D9%82-1564103914328.jpg '.此内容也应通过 HTTPS 提供。

这个错误:跨源读取阻止 (CORB) 阻止了跨源响应 http://twassol.herokuapp.com/images/%D8%A8%D8%B7%D8%A7%D8%B1%D9%8A%D9%82-1564103914328.jpg使用 MIME 类型文本/html。参见 https://www.chromestatus.com/feature/5629709824032768了解更多详情。

我尝试使用“http://twassol.herokuapp.com/”,它上传了图片但没有显示。

这是 post.service.ts 文件:

import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import { Subject } from 'rxjs';
import { map } from 'rxjs/operators';

import { Post } from './post.model';
import { environment } from '../../environments/environment';

const BACKEND_URL = environment.apiUrl + '/posts/';

@Injectable({providedIn: 'root'})

export class PostsService {
private posts: Post[] = [];
private postsUpdated = new Subject<{posts: Post[], postsCount: number}>();

constructor(private http: HttpClient, private router: Router){}

getPosts(postsPerPage: number, currentPage: number) {
const queryParams = `?pagesize=${postsPerPage}&page=${currentPage}`
this.http.get<{ message: string, posts: any, maxPosts: number }>(BACKEND_URL + queryParams)
.pipe(map( postData => {
return { posts :postData.posts.map(post =>{
return{
title: post.title,
content: post.content,
id: post._id,
imagePath: post.imagePath,
creator: post.creator
}
}), maxPosts: postData.maxPosts};
}))
.subscribe((transformedPostData) => {
this.posts = transformedPostData.posts;
this.postsUpdated.next({posts: [...this.posts], postsCount: transformedPostData.maxPosts});
});
}
getPostUpdateListener(){
return this.postsUpdated.asObservable()
}

getPost(id: string){
return this.http.get<{_id: string, title: string, content: string, imagePath: string, creator: string}>(
BACKEND_URL + id
);
}

addPost(title: string, content: string, image: File){
const postData = new FormData;
postData.append('title', title);
postData.append('content', content);
postData.append('image', image, title);
this.http.post<{message: string, post: Post}>(BACKEND_URL, postData)
.subscribe( responseData => {
this.router.navigate(['/']);
});
}

updatePost(id: string, title: string, content: string, image: string | File){
let postData: Post | FormData;
if (typeof(image) === 'object'){
postData = new FormData;
postData.append('id', id);
postData.append('title', title);
postData.append('content', content);
postData.append('image', image, title);
} else {
postData = {
id: id,
title: title,
content: content,
imagePath: image,
creator: null
}
}

this.http
.put(BACKEND_URL + id, postData)
.subscribe(response => {
this.router.navigate(['/']);
});
}

deletePost(postId: string){
return this.http.delete(BACKEND_URL + postId)
}
}

最佳答案

将你的图片转换成base64字符串并在post请求中传递

html:

 <button type="button" (click)="upload.click()" >
<span>Upload Photo</span>
</button>
<input #upload formControlName="imageData" type="file" name="imageData" accept='image/*' (change)="getFiles($event)"style="display:none;">

:

      getFiles(event) {
this.files = event.target.files;
var reader = new FileReader();
reader.onload = this._handleReaderLoaded.bind(this);
reader.readAsBinaryString(this.files[0]);
}

_handleReaderLoaded(readerEvt) {
var binaryString = readerEvt.target.result;
this.base64DataOfImage= btoa(binaryString);
this.{{your Model}}.imageData = this.base64DataOfImage
this.fileSrc = 'data:image/png;base64,'+this.base64DataOfImage;
}

从服务中取回数据

html

<img  height="200" [src]="fileSrc" />

//service call(assuming the returned as {"imageData":"base64data"})

this.fileSrc='data:image/png;base64,'+responce.imageData;

确保在后端增加请求正文大小

app.use(bodyParser.json({limit: "50mb"}));
app.use(bodyParser.urlencoded({limit: "50mb", extended: true, parameterLimit:50000}));

关于node.js - 如何从 Angular 发送https post请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57223416/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com