gpt4 book ai didi

jQuery 文件上传 "Error - Empty file upload result"- Rails 应用程序

转载 作者:行者123 更新时间:2023-12-03 22:40:06 25 4
gpt4 key购买 nike

我不明白为什么会出现此错误?我已经从源代码示例中完全复制了。我能够加载所有内容,然后当我尝试上传(通过单击“开始”)时,出现此错误。

enter image description here

但即使我收到此错误,它仍然可以正确上传到我的数据库。当我刷新页面时,我有一个显示页面,将显示上传的图像,它就在那里。是什么导致了这个问题?

请帮忙!我也用回形针上传。在我的环境中,我在本地上传,但在生产中,我上传到 Amazon S3

这就是我的 Controller 在创建或更新表单时的样子。

def create
@project = Project.new(project_params)

respond_to do |format|
if @project.save

if params[:photos]
params[:photos].each { |image|
@project.project_images.create(photo: image)
}
end
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end

def update
@project = current_user.projects.find(params[:id])

respond_to do |format|
if @project.update(project_params)

if params[:photos]
params[:photos].each { |image|
@project.project_images.create(photo: image)
}
end

format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { render :show, status: :ok, location: @project }
else
format.html { render :edit }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end

编辑:

这是我的表格

<%= simple_form_for @project, html: { multipart: true, id: 'fileupload' } do |f| %>

<span class="btn btn-success fileinput-button">
<i class="glyphicon glyphicon-plus"></i>
<span>Add files...</span>
<input type="file" name="photos[]" id='photo_upload_btn', multiple>
</span>
<button type="submit" class="btn btn-primary start">
<i class="glyphicon glyphicon-upload"></i>
<span>Start upload</span>
</button>
<button type="reset" class="btn btn-warning cancel">
<i class="glyphicon glyphicon-ban-circle"></i>
<span>Cancel upload</span>
</button>

<% end %>

编辑:

jQuery 代码

<script>
$(function () {

'use strict';

// Initialize the jQuery File Upload widget:
$('#fileupload').fileupload();
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
});
// Load existing files:
$('#fileupload').addClass('fileupload-processing');
$.ajax({
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
url: $('#fileupload').fileupload('option', 'url'),
dataType: 'json',
context: $('#fileupload')[0]
}).always(function () {
$(this).removeClass('fileupload-processing');
}).done(function (result) {
$(this).fileupload('option', 'done')
.call(this, $.Event('done'), {result: result});
});
});
</script>

<script src="http://blueimp.github.io/JavaScript-Templates/js/tmpl.min.js"></script>
<script src="http://blueimp.github.io/JavaScript-Load-Image/js/load-image.all.min.js"></script>
<script src="http://blueimp.github.io/JavaScript-Canvas-to-Blob/js/canvas-to-blob.min.js"></script>
<script src="http://blueimp.github.io/Gallery/js/jquery.blueimp-gallery.min.js"></script>

最佳答案

jQuery File Upload已指定 JSON 响应 mentioned here

扩展您的创建方法以返回类似于以下输出的 JSON 响应:

 {"files": [
{
"name": "picture1.jpg",
"size": 902604,
"url": "http:\/\/example.org\/files\/picture1.jpg",
"thumbnailUrl": "http:\/\/example.org\/files\/thumbnail\/picture1.jpg",
"deleteUrl": "http:\/\/example.org\/files\/picture1.jpg",
"deleteType": "DELETE"
},
{
"name": "picture2.jpg",
"size": 841946,
"url": "http:\/\/example.org\/files\/picture2.jpg",
"thumbnailUrl": "http:\/\/example.org\/files\/thumbnail\/picture2.jpg",
"deleteUrl": "http:\/\/example.org\/files\/picture2.jpg",
"deleteType": "DELETE"
}
]}

此外,如果出现错误,只需向各个文件对象添加错误属性即可:

{"files": [
{
"name": "picture1.jpg",
"size": 902604,
"error": "Filetype not allowed"
},
{
"name": "picture2.jpg",
"size": 841946,
"error": "Filetype not allowed"
}
]}

最简单的方法是将以下代码添加到您的照片模型 see here

  def to_jq_upload
{
"name" => read_attribute(:attachment_file_name),
"size" => read_attribute(:attachment_file_size),
"url" => attachment.url(:original),
"thumbnailUrl" => attachment.url(:thumb),
"deleteUrl" => "/photos/#{self.id}",
"deleteType" => "DELETE"
}
end

你的 JSON 响应应该是这样的 see here

format.json { render json: {files: [@photo.to_jq_upload] }}

关于jQuery 文件上传 "Error - Empty file upload result"- Rails 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31800345/

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