# 导入过程中上传头像遇到问题

**URL:** https://meta.discourse.org/t/issue-with-upload-avatar-during-import/84227
**Category:** Development
**Created:** [2018年三月30日 13:41 UTC](https://meta.discourse.org/t/issue-with-upload-avatar-during-import/84227 "2018-03-30T13:41:08Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Cara](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@Cara](https://meta.discourse.org/u/Cara)
#### Post date: [2018年三月30日 13:41 UTC](https://meta.discourse.org/t/issue-with-upload-avatar-during-import/84227/1 "2018-03-30T13:41:09Z")

</div>

HI,  
I’m trying to import avatar for users. I do this in the punbb.rb import file:

```
create_users(results, total: total_count, offset: offset) do |user|
        { id: user['id'],
          email: user['email'],
          username: user['username'],
          name: user['name'],
          created_at: Time.zone.at(user['created_at']),
          website: user['website'],
          registration_ip_address: user['registration_ip_address'],
          location: user['location'],
          moderator: user['group_id'] == 4,
          admin: user['group_id'] == 1 ,
          post_create_action: proc do |newuser|
            origname=user['avatar_file']
            path='var/www/discourse/avatars'
            upload = create_upload(newuser.id, path, origname)
            if @upload
              newuser.create_user_avatar
              newuser.user_avatar.update(custom_upload_id: upload.id)
              newuser.update(uploaded_avatar_id: upload.id)
            else
              puts "Upload failed."
            end
          end
        }

```

I’m noob regarding Ruby, I don’t understand where it doesn’t work. “Upload failed” even for user I know I have upload the correct file to ‘var/www/discourse/avatars’…

Thanks for your support

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2018年三月31日 12:31 UTC](https://meta.discourse.org/t/issue-with-upload-avatar-during-import/84227/2 "2018-03-31T12:31:48Z")

</div>

I think the error should be in the output. My guess is that the path is wrong. Perhaps you need a / at the start of the directory?

---

<div class="post-metadata">

### Author: ![Cara](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@Cara](https://meta.discourse.org/u/Cara)
#### Post date: [2018年三月31日 16:48 UTC](https://meta.discourse.org/t/issue-with-upload-avatar-during-import/84227/3 "2018-03-31T16:48:33Z")

</div>

I tried, it didn’t work. Thanks 🙂

---

<div class="post-metadata">

### Author: ![Cara](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@Cara](https://meta.discourse.org/u/Cara)
#### Post date: [2018年四月2日 06:18 UTC](https://meta.discourse.org/t/issue-with-upload-avatar-during-import/84227/4 "2018-04-02T06:18:26Z")

</div>

I finally achieve the upload like this:

```
create_users(results, total: total_count, offset: offset) do |user|
        { id: user['id'],
          email: user['email'],
          username: user['username'],
          name: user['name'],
          created_at: Time.zone.at(user['created_at']),
          website: user['website'],
          registration_ip_address: user['registration_ip_address'],
          location: user['location'],
          #moderator: user['group_id'] == 4,
          #admin: user['group_id'] == 1 ,
          post_create_action: proc do |newuser|
            path=File.join("/var/www/discourse/avatars", user['avatar_file'])
            if File.exists?(path)
              upload = create_upload(newuser.id, path, File.basename(path))
              if upload.nil?
                puts "Upload failed. Path #{path} Id New #{newuser.id}"
              else
                newuser.create_user_avatar
                newuser.user_avatar.update(custom_upload_id: upload.id)
                newuser.update(uploaded_avatar_id: upload.id)
              end
            end
          end
        }

```

Thanks for the help and best regards! 🙂

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [2018年四月3日 06:18 UTC](https://meta.discourse.org/t/issue-with-upload-avatar-during-import/84227/5 "2018-04-03T06:18:29Z")

</div>

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
