Uploading files: FileUpload.SaveAs or manually wri

2019-08-01 06:35发布

问题:

Using the FileUpload control, please explain the difference between the following two methods of uploading file:

1. Using the FileUpload.SaveAs() method:

fileUploadControl.SaveAs(path)

2. Writing a byte array to disk from FileUpload.FileBytes using File.WriteAllBytes():

File.WriteAllBytes(path, fileUploadControl.FileBytes);

How would these compare when uploading large files?

回答1:

These both have different purposes.
SaveAs lets you save as a file directly while WriteAllBytes gives you a byte array of contents.

Your file upload control will receive the bytes only after the file has been uploaded by the client, so there will be no difference in upload speeds.

A byte array is a value-type, so if you are passing copies of that around, note that it will create copies in memory whenever you pass it to functions.

I would use FileUpload.FileBytes when I want to access the bytes directly in memory and fileUploadControl.SaveAs whenever all I want to do is write the file to disk.