/ / रेल Errno :: ENOENT: अपलोड को सहेजते समय ऐसी कोई फ़ाइल या निर्देशिका नहीं है - रूबी-ऑन-रेल, रूबी, रूबी-ऑन-रेल्स -3, nginx, amazon-ec2

रेल Errno :: ENOENT: अपलोड को सहेजते समय ऐसी कोई फ़ाइल या निर्देशिका नहीं - रूबी-ऑन-रेल, रूबी, रूबी-ऑन-रेल-3, nginx, amazon-ec2

मैं अपने रेल एपीआई के साथ निम्नलिखित समस्या हूँ।

मैं एक अस्थायी निर्देशिका में एक फ़ाइल को बचाने की कोशिश कर रहा हूँ। मेरी शुरुआती धारणा यह थी कि मुझे इसे सिस्टम में सहेजने में सक्षम होना चाहिए /tmp निर्देशिका, क्योंकि यह उस निर्देशिका के लिए है। इसलिए मेरे पास निम्नलिखित कोड है gallery_images_controller.rb:

    def upload
# set the upload
upload = params[:qqfile].original_filename
# we want to save files in tmp for now
directory = "/tmp"
ap(directory)
# create the full path for where to save the file
path = File.join(directory, upload)
ap(path)
# get the md5 of the file so we can use it as the key in the json response
md5 = Digest::MD5.file(path).hexdigest
# save the file
File.open(path, "w+b") { |f| f.write(params[:qqfile].read) }
# render the result with the md5 as the key and the filename and full path
render json: {success: true, upload: {"#{md5}": {filename: upload, full_path: path}}}, status: 200
end

जब मुझे फ़ाइल के साथ पोस्ट अनुरोध भेजा जाता है तो मुझे निम्नलिखित त्रुटि मिलती है:

{:error=>true, :message=>"Errno::ENOENT: No such file or directory @ rb_sysopen - /tmp/3Form-Museum-of-Science-25.jpg"}

मैंने फ़ाइल को Rails.root tmp फ़ोल्डर में सहेजने और उसी त्रुटि को प्राप्त करने का भी प्रयास किया:

directory = "#{Rails.root}/tmp/"

{:error=>true, :message=>"Errno::ENOENT: No such file or directory @ rb_sysopen - /vagrant/tmp/3Form-Museum-of-Science-25.jpg"}

मैं भी मोड के रूप में की कोशिश की है w+bतथा wb कोई फायदा नहीं।

रूबी के लिए प्रलेखन (http://ruby-doc.org/core-2.2.3/IO.html#method-c-new) का कहना है कि ए w तथा w+ अगर यह मौजूद नहीं है, तो मोड को फाइल बनाना चाहिए। यह वही है जो मैं इसे करना चाहता हूं, और यह नहीं है।

इसके अलावा, मैंने फ़ोल्डर्स पर अनुमतियों की जांच की है। /tmp फ़ोल्डर, जैसा कि आप उम्मीद करेंगे, 777 और रेल रूट है /vagrant/tmp रेल रूट में अन्य सभी फ़ोल्डरों की तरह 755 है।

कृपया सहायता कीजिए!


प्रणाली की जानकारी:

  • देव: वग्रांत बॉक्स ubuntu 14.04, यूनिकॉर्न और नगनेक्स चल रहा है
  • उत्पाद: अमेज़न EC2 ubuntu 14.04, यूनिकॉर्न और नेग्नेक्स चल रहा है

उत्तर:

उत्तर № 1 के लिए 1

आपको बस दौड़ना चाहिए

File.open(path, "w+b") { |f| f.write(params[:qqfile].read) }

से पहले

md5 = Digest::MD5.file(path).hexdigest

बस दो पंक्तियों को स्वैप करें ताकि हेक्स डाइजेस्ट की गणना करने से पहले फ़ाइल बनाई जाए


जवाब के लिए 0 № 2

किसी के लिए भी जो इसे यहाँ इसी मुद्दे के साथ बनाता है, वही मैंने इसे ठीक करने के लिए किया था।

2 मुद्दे थे जिन्हें मैंने इसे हल करने के लिए तय किया था:

  1. मैंने इस लाइन को बदल दिया:

    File.open (पथ, "w + b") {| f | f.write (परमेस: [qqfile] .read)}

इसके लिए:

File.open(path, "w+b") { |f| f.write(path) }
  1. ऊपर की लाइन को आगे बढ़ाया ऊपर the md5 लाइन

तो अब मेरा अपलोड फ़ंक्शन इस तरह दिखता है:

def upload
# set the upload
upload = params[:qqfile].original_filename
# we want to save files in tmp for now
directory = "/tmp"
# create the full path for where to save the file
path = File.join(directory, upload)
# save the file
File.open(path, "w+b") { |f| f.write(path) }
# get the md5 of the file so we can use it as the key in the json response
md5 = Digest::MD5.file(path).hexdigest
# render the result with the md5 as the key and the filename and full path
render json: {success: true, upload: {"#{md5}": {filename: upload, full_path: path}}}, status: 200
end