开发者

Performing a Process on an Uploaded File .NET

I'm programming in ASP.NET 3.5.

I need to process a file that has to be uplo开发者_StackOverflow社区aded by a user. The file is in a specific format. Basically, I need to take the information in the file, parse it, and insert it into a SQL db. I have no problem doing that, but I was wondering how to do this from a web site.

I think I want to create a share that the user will have access to, have the user upload the file onto the share, and upon upload, save a record into the DB with the full UNC path. In the site, process the file with that UNC path, then move the file to some "processed" folder.

Has anyone done something like this? Is there any "standard" way of doing things similar to this?

Thanks.


I don't know if this can help you, but you can use the FileUpload Class in the namespace System.Web.UI.WebControls.

You can check MSDN: here


1) Create an ASPX page with upload INPUT.

2) Let the user upload the file

3) Get the ASP.NET uploaded file and write the binary streams to a file and also Register the file to a database as well

4) Finish the request but before doing that spawn a background thread to process the file

5) You need a separate cleanup operation fro those files that were registered but failed to be processed for whatever reason.


Quickest solution - if your file processing will complete before the script timeout, and you're happy to have the user wait while it happens:

Map a virtual directory on your website to your upload folder - on our sites, we have /~upload/ on all our web servers mapped to an open share on a fileserver in our DMZ.

Then use code something like this in your page code-behind

protected void ProcessFilesButton_Click(object sender, EventArgs e) {
    string uploadRootPath = Server.MapPath("~/upload/"); 

    foreach(var uploadedFile in Request.Files) {

      // might want to use a GUID filename here instead
      // to avoid risk of filename conflicts.
      string bareFileName = Path.GetFileName(uploadedFile.FileName);
      string fullFilePath = Path.Combine(uploadRootPath, bareFileName);
      uploadedFile.SaveAs(fullFilePath);
      myMagicFileProcessor.ProcessFile(fullFilePath);
   }
   MyLabel.Text = String.Format("{0} files processed", Request.Files.Count);
}

More complex solutions would involve spawning a background thread (or a separate Windows service or scheduled task) to handle the file processing, and then returning a "Please Wait..." page with a meta-refresh on it, that will redraw the page every, say, 10 seconds, and then in the code-behind you query the status of the files in the queue and report on them as processing completes for each one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜