[英]Angular 4 Rails 5 paperclip file upload
我正在使用Angular 4前端和Rails 5 api后端進行項目。 我正在嘗試使用回形針上傳視頻,但是我不知道為什么它沒有保存到數據庫中。 日志中仍然存在所有參數,但仍不保存。 我修改了控制器,取出了require語句,並重構了前端代碼的服務器時間。 我曾嘗試過多種服務器技術,但各種方法均未奏效,而我正在為正在發生的事情畫上空白。 任何投入將不勝感激。
這是我看到的日志
I, [2017-11-22T19:21:10.984681 #5898] INFO -- : [6c0fdb67-31b6-43c6-8a79-e5ed2dfbec1e] Started POST "/movies" for 108.71.214.220 at 2017-11-22 19:21:10 +0000
I, [2017-11-22T19:21:11.001990 #5898] INFO -- : [6c0fdb67-31b6-43c6-8a79-e5ed2dfbec1e] Processing by MoviesController#create as HTML
I, [2017-11-22T19:21:11.002088 #5898] INFO -- : [6c0fdb67-31b6-43c6-8a79-e5ed2dfbec1e] Parameters: {"video"=>#<ActionDispatch::Http::UploadedFile:0x000055a94d4e5970 @tempfile=#<Tempfile:/tmp/RackMultipart20171122-5898-17o86vd.mp4>, @original_filename="SampleVideo_720x480_1mb.mp4", @content_type="video/mp4", @headers="Content-Disposition: form-data; name=\"video\"; filename=\"SampleVideo_720x480_1mb.mp4\"\r\nContent-Type: video/mp4\r\n">, "title"=>"Test Movie", "year"=>"1998", "plot"=>"Awesomeness"}
D, [2017-11-22T19:21:11.016579 #5898] DEBUG -- : [6c0fdb67-31b6-43c6-8a79-e5ed2dfbec1e] ^[[1m^[[36mApiKey Load (0.3ms)^[[0m ^[[1m^[[34mSELECT "api_keys".* FROM "api_keys" WHERE "api_keys"."client" = $1 LIMIT $2^[[0m [["client", "z8CSVtE3qejMxs4FFwYmKA"], ["LIMIT", 1]]
I, [2017-11-22T19:21:11.021183 #5898] INFO -- : [6c0fdb67-31b6-43c6-8a79-e5ed2dfbec1e] Redirected to http://localhost:4200
I, [2017-11-22T19:21:11.021266 #5898] INFO -- : [6c0fdb67-31b6-43c6-8a79-e5ed2dfbec1e] Filter chain halted as :authorized rendered or redirected
I, [2017-11-22T19:21:11.021376 #5898] INFO -- : [6c0fdb67-31b6-43c6-8a79-e5ed2dfbec1e] Completed 302 Found in 19ms (ActiveRecord: 5.5ms)
模板
<div class="container">
<h1>Movie Add Form</h1>
<form [formGroup]="newForm" (ngSubmit)="upload()">
<div class="form-group">
<label for="title">Title:</label>
<input
type="text"
name="title"
class="form-control"
formControlName="title"
>
<label for="year">Year:</label>
<input
type="text"
name="year"
class="form-control"
formControlName="year"
>
<label for="plot">Plot:</label>
<input
type="text"
name="plot"
class="form-control"
formControlName="plot"
>
</div>
<div class="form-group">
<input type="file" #fileInput placeholder="Upload file..." accept="video/mp4">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
零件
export class DvdNewComponent implements OnInit {
newForm: FormGroup
@ViewChild('fileInput') fileInput;
constructor(private dvdService: DvdService,
private router: Router) { }
ngOnInit() {
this.newForm = new FormGroup({
'title': new FormControl(null, Validators.required),
'year': new FormControl(null, Validators.required),
'plot': new FormControl(null, Validators.required)
})
}
upload() {
const movieFile = this.fileInput.nativeElement.files[0];
this.dvdService.uploadMovie(movieFile, this.newForm.value)
.subscribe(
(data) => {
console.log('data ' + data)
},
(err: HttpErrorResponse) => {
console.log(err)
},
() => {
this.router.navigate(['/library'])
}
)
}
}
服務
uploadMovie(fileToUpload: File, form): Observable<Movie> {
const formData = new FormData();
formData.append('video', fileToUpload)
formData.append('title', form.title)
formData.append('year', form.year)
formData.append('plot', form.plot)
const headers = new Headers();
headers.delete('Content-Type');
headers.append('access-token', this.tokenService.currentAuthData.accessToken)
headers.append('client', this.tokenService.currentAuthData.client)
const options = new RequestOptions({ headers: headers });
return this.http.post(this.moviesURL + '/movies', formData, options)
.map((res) => res.json())
}
支持的控制器
def create
movie = Movie.new(movie_params)
if movie.save
render json: movie, status: 201
else
render json: { errors: movie.errors }, status: 422
end
end
def movie_params
params.permit(:title, :year, :plot, :video, :video_url)
end
閱讀評論后,我檢查了機架式芯線,發現它已啟用。 接下來,我檢查了授權系統,經過一番挖掘后發現客戶端ID不匹配。 這樣做是因為前端被路由到錯誤的端點。 因此,我通過插入正確的端點解決了問題,現在代碼可以正常工作了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.