9

I need to upload my code and put it in my question. How can I do that?

If there's no way to do so, then show me an alternative way (for example, a website I can upload my file and then use its link on Stack Overflow).

8
  • Read this documentation it will help you to ask a good question with details Commented Jul 8, 2019 at 14:33
  • 6
    If your code is too large to fit in a question, then this site is likely the wrong place to put it. Please create a [mre] Commented Jul 9, 2019 at 7:16
  • 5
    Also see How to create a Minimal, Reproducible Example. Commented Jul 9, 2019 at 7:53
  • 1
    I'm voting to reopen this question because it is primarily about whether SO supports file attachments, and only secondarily about formatting code as text. The two targets fail to ask or address the primary question here whatsoever, and thus are not accurate, let alone good, duplicate targets. Commented Aug 26, 2022 at 19:49
  • @TylerH The meta.stackoverflow.com/questions/307322/… question is on point. The OP here is asking where to upload their code. That question is about how to upload their MRE. They're pretty much exactly the same. And rene's answer to it answers this question too; you can't upload files to this site. You can upload it to another site and provide a link, but the core code should be on this site. Commented Aug 26, 2022 at 22:15
  • @HereticMonkey This question serves much better as a more broad target given that it asks about files in general rather than just code, and has a much better answer. Frankly the question from 2015, while older, should be closed as a duplicate of this one, instead. Commented Aug 26, 2022 at 22:19
  • @TylerH "I need to upload my code" how does that ask about "files in general rather than just code"? I could see merging the two if all you want to do is get the answers together. This is a horrible question. Two sentences demanding a method for uploading their code? Commented Aug 26, 2022 at 22:24
  • @HereticMonkey "How can I upload a file to Stack Overflow?" Commented Aug 29, 2022 at 13:33

1 Answer 1

10

Stack Overflow don't allow you to upload files as attachments to a question or answer. Instead, it asks for an MRE, or Minimal Reproducible Example.

You are required to debug your code (which is causing the error) and find just the specific section of the code that is making a problem (this is the "minimal" part of MRE). Make sure it is enough of the code that others can take that code and run it in their own environment and get the same outcome (this is the "reproducible" part of MRE).

Now copy that section of code and prepare to add it to your question. If that code is HTML, CSS, or JavaScript, then you can click this button to create a runnable "snippet" of code:

Enter image description here

If the code is in any other programming language, then you can click this button, instead:

Enter image description here

If your code uses or requires a data set in order to run or produce the necessary results, you are recommended to upload that data to a site like Pastebin or, if it's not text data, to a file-hosting site. Then you can link to the URL for your file upload in the post.

Note that you will be required to include your code before you can share a URL if you are new member. URLs without code are not allowed, and attempts to fake code content in order to post a URL may result in a question being negatively received.

4
  • 9
    I understand the part about not putting a whole file of code, but what about a file with some data ? Sometimes to run the OP's minimal example you need their input, typically looking at this question, OP had no choice but to upload an image of his data. Commented Feb 10, 2021 at 9:58
  • True, exceptions exist everywhere :) Commented Feb 10, 2021 at 16:09
  • Someone just told me I need to attach the input my MRE needs. That input has to be a file (a zipped folder of an image and a video with metadata). How can I attach that file to the stackoverflow question? Commented Oct 18 at 11:17
  • 1
    @Nils If it is a project, you should share it in a public github repo. If not too large, a zip file could be shared there too. The last resort is putting a public link to Google Drive. Keep note in any case if a resource external to StackOverflow gets delete, it hurts the future readers of the QnA. To keep context in place, please try to avoid hosting things on external services, and also try to reduce what's actually needed for a MRE to a bare minimum. Commented Oct 18 at 12:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.