Newbie - Upload pdf file - iframe error

Hello everyone, I started quite recently with Go and I’m already a huge fan.

But I am currently working on an WebApplication, where users should be able to upload PDF files and these can be opened through the browser (PDF viewer).

My problem:
If I try to use ioutil.TempFile() the file is damaged and I cannnot open it through the browser and with my locally installed pdf reader. If I try to catch the file, use ioutil.ReadAll() and write all the data with into a new file with ioutil.WriteFile(), then the file is fine, but i can still not open it through my website.
Can me tell someone, how to access the tmpfile information of an multipart.file?

if r.Method == http.MethodPost {
	r.ParseMultipartForm(maxUploadSize)

	file, handler, err := r.FormFile("myFile")
	if err != nil {
		utils.InternalServerError(w)
		return
	}
	defer file.Close()

            data, err2 := ioutil.ReadAll(file)
	if err2 != nil {
		utils.LoggingWarningFile(err2.Error())
		return
	}

	path = temp + "\\" + itemname + fileExt

	err2 = ioutil.WriteFile(path, data, 0777)
	if err2 != nil {
		fmt.Println(err2)
		return
    }
}

I would be very happy for every tip or solution approach.
Thanks

Even though I had some troubles on the way, but at the end, I found an workaround to grab and store the file.

data, err2 := ioutil.ReadAll(file)
utils.CheckError(err2)
err2 = ioutil.WriteFile(path,data,0644)
utils.CheckError(err2)

After that little accomplishment, I tried to open it with the PDFViewer. I am still not happy with my solution, but for now it is an nice workaround:

f, err := os.Open(path)
utils.CheckError(err)
defer f.Close()

w.Header().Set("Content-type", "application/pdf")
if _, err := io.Copy(w, f); err != nil {
 utils.LoggingErrorFile(err.Error())
 w.WriteHeader(500)
}

Maybe I will be able to help someone else :smiley:

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