I'm trying to get the AjaxFileUpload control working and it's giving me some strange problems. It looks like when I call SaveAs the file is just being deleted and not saved in the directory I specify.

Protected Sub AjaxFileUpload1_OnUploadComplete(ByVal sender As Object, ByVal e As AjaxControlToolkit.AjaxFileUploadEventArgs) Handles AjaxFileUpload1.UploadComplete
Try
       Dim filePath As String = "C:\Inetpub\wwwroot\domain2\files\" & Convert.ToString(e.FileName)
       If e.FileSize > 72000 Then
           Return
       Else
           ajaxFileUpload.SaveAs(filePath)
       End If
Catch ex As Exception
      Throw ex
End Try
End Sub

The file uploads on to the server in to c:windows\temp_ajaxfileupload\somerandomstring\test.txt but disappears once ajaxFileUpload.SaveAs(filePath) is called and then i get a "Can not find the file c:windows\temp_ajaxfileupload\somerandomstring\test.txt" error. I'm uploading from one website and trying to save to another site on the same server with the same IUSR.

Dani AI

Generated

— quick diagnosis and practical checks based on your symptom (file lands in the Ajax temp folder then disappears when you call SaveAs and you get "Cannot find the file ..._AjaxFileUpload...\test.txt").

AjaxFileUpload’s SaveAs moves the uploaded temp file into your destination and the control/handler also cleans up the temporary data after a successful save. If the SaveAs operation cannot complete (for example because the process lacks write access) you can end up with the temp file gone or a FileNotFound error when the control tries to finish its cleanup. (c-sharpcorner.com)

Most likely cause when saving into a different site folder is a permissions/identity mismatch. Anonymous/IUSR is not the same as the worker process identity — the web app runs under its application pool identity (IIS AppPool\<YourPoolName>) by default, and that identity must have NTFS write/modify permission on the target folder. Grant the uploading app’s app-pool identity explicit ACLs (or put the files into a shared folder that both apps can access) rather than relying on IUSR. Example (run as admin):

ICACLS "C:\Inetpub\wwwroot\domain2\files" /grant "IIS AppPool\YourSourceAppPoolName":M

See Microsoft guidance on app-pool identities and ACLs. (learn.microsoft.com)

Concrete troubleshooting steps

  • Confirm AjaxFileUpload handler is configured in web.config (the control uses AjaxFileUploadHandler.axd). If the handler isn’t set up things will behave oddly. (aspsnippets.com)
  • Test a simple write to the destination using the same app (to prove ACLs): write a small file from your UploadComplete handler. If that fails, it’s an ACL issue.
  • Alternative to SaveAs: read the uploaded bytes and write them yourself (this avoids any magic cleanup timing issues), then explicitly delete temp if needed:
    Dim bytes = e.GetContents()
    System.IO.File.WriteAllBytes(targetPath, bytes)
    e.DeleteTemporaryData()

    (Use DeleteTemporaryData only if you didn’t call SaveAs; see guidance). (stackoverflow.com)

Other practical notes

  • Don’t rethrow with Throw ex (it resets the original stack trace); use Throw or log then Throw.
  • If you must save into another site’s application folder, prefer a shared folder or an API on the destination site so you avoid cross-app ACL surprises.
  • Check the real exception (log it) rather than swallowing and rethrowing — that gives the exact cause (permission, path, disk, etc.).

If the above checks still leave the same error, post the exact exception message/stack (from logged output) and the app-pool names for both sites — that will let others narrow whether it’s ACL, handler/timing, or an infrastructure issue.

That should say

ajaxFileUpload1.SaveAs(filePath)

btw.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.