Uploading files: FileUpload.SaveAs or manually wri

2019-08-01 06:49发布

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条回答
Luminary・发光体
2楼-- · 2019-08-01 07:31

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.

查看更多
登录 后发表回答