0

Been looking this up. Was using IE before and understand the full path was not secure. I am using C# on Asp. Net Core (I am very new to this). I need to get the file (which will be a zip file) and iterate through the zip file for it's contents. However on Chrome and Edge the value returned from a Input type file is just the file name, so I am stuck on what I can do to iterate through the inputted file. Any help is appreciated.

To help I am just using simple form:

<form method="post">
<input type="file" name="varName">
<input type="submit">
</form>
  • 1
    If you don't show us the code that fails it is practically impossible to help. – Steve Jul 07 '22 at 20:25
  • Does this answer your question? [How to upload files in ASP.NET Core?](https://stackoverflow.com/questions/35379309/how-to-upload-files-in-asp-net-core) – Sergey Vyacheslavovich Brunov Jul 07 '22 at 20:29
  • I didn't think it is necessary. I am using C# on ASP. NET CORE, and using a chrome browser. When I use a HTML form of input type=file, the return value is just the file name. But for IE, it returns the full file path. So my question is, is there anyway to access the file without knowing the entire file path? Since chrome only returns the file name. – Temp Account Jul 07 '22 at 20:30
  • I hope so, I will give it a shot. Sorry this is my first time posting, and Im very new to C# and ASP NET CORE, still learning everything. – Temp Account Jul 07 '22 at 20:31
  • _"My name is Temp Account just because using Email called Temp Account, so apologies"_ @TempAccount , you can change your SO "Display Name", see https://meta.stackoverflow.com/questions/268879/how-can-i-change-my-username-on-stack-overflow – Stefan Wuebbe Jul 07 '22 at 20:51
  • Hi @Temp Accout, what do you want to do when you know the file path? – Rena Jul 08 '22 at 05:15
  • So my goal would be to read through the file (it is a zipped file) and then move the file to another location. But I am not sure on how to even access this file. – Temp Account Jul 11 '22 at 12:37
  • Hi @TempAccount, Chrome just give the file name which we cannot change it for it is by design. For security reason, although you use js to get the value of the file input, it will give you value like:`C:\fakepath\some_file`. So what you want is not possible to come out by asp.net core. But you said `I am stuck on what I can do to iterate through the inputted file.`, it seems you want to move the file in the zip file, you can refer to: https://stackoverflow.com/a/69483170/11398810 – Rena Jul 12 '22 at 05:59

1 Answers1

0

When you select a file using <input type="file"> it will show the name of the file and some metadata like size, last modified etc. This is optimal because it would be too much to load the whole content of file unnecessarily.

However, you can read the data inside the browser using FileReader() API like this:

 const reader = new FileReader();
  reader.onload = function fileReadDone() {
    console.log(reader.result);
  };
  reader.readAsText(event.target.files[0])
//Or to read as dataURL, use reader.readAsDataURL(event.target.files[0])
janmejay
  • 44
  • 3