P
P
Pavel Chuev2016-02-09 20:52:45
JavaScript
Pavel Chuev, 2016-02-09 20:52:45

How to upload an image to the VK server?

I wrote an image parser, which for now just downloads the necessary pictures into a folder. Further, according to the plan, any image from the folder is uploaded to the VK server using a link received in advance via the API. How to implement on node?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AltQ, 2016-02-10
@AllDecay

You can create an array "files" and add paths to files (no more than five) to it, as well as parse the resulting link with the "parse" function of the "url" module, writing the result to the "URL" variable. Further code could be like this:

boundary = "H#pB$Ky6Vs+Q8ioY_buA"
data = ""

for (i = 0; i < files.length; i++) {data += "--" + boundary + "\r\nContent-Disposition:form-data;name=file" + (i + 1) + ";filename=.png" + "\r\n\n" + fs.readFileSync(files[i]).toString("binary") + "\r\n"}

data += "--" + boundary + "--"

request = http.request(
  {
    method: "POST",
    path: URL.path,
    host: URL.host,
    headers: {"Content-Type": "multipart/form-data;boundary=" + boundary}
  },

  function (response) {
    var data = ""

    response.on("data", function (chunk) {data += chunk})
    response.on("end", function () {/* здесь можно распарсить ответ сервера и получить ссылки на изображения без использования метода «photos.save» */})
  }
)

request.write(data, "binary")
request.end()

E
EVAS, 2016-02-10
@EVAS

You can do it like this)
1) Write an application authorization script. So that he can get the rights https://vk.com/dev/permissions . Or find a ready-made module, by the way, there are a lot of them in npm)
2) Choose a method for uploading an image, most likely this is https://vk.com/dev/photos.getUploadServer com/dev/methods

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question