[英]Can't get asp.net upload file example to work on IIS local network
[英]Saving Excel File not work in IIS 7 although it works in local ASP.NET App
我試圖用數據庫中的記錄保存excel文件。 它在asp.net應用程序中運行良好。 但是,當我將其移至IIS服務器時,它不起作用。 我不知道如何從IIS調試應用程序。
這是一些代碼:
string str, filename;
connection = new SqlConnection(con_dm);
connection.Open();
str = "SELECT * FROM Person";
SqlDataAdapter adp = new SqlDataAdapter(str, connection);
System.Data.DataTable dataTable = new System.Data.DataTable();
adp.Fill(dataTable);
if (dataTable.Rows.Count > 0)
{
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
oXL = new Excel.Application();
oXL.Visible = false;
oXL.SheetsInNewWorkbook = 1;
oWB = (Excel._Workbook)(oXL.Workbooks.Add());
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
try
{
string[] colNames = new string[dataTable.Columns.Count];
int col = 0;
foreach (DataColumn dc in dataTable.Columns)
colNames[col++] = dc.ColumnName;
char lastColumn = (char)(65 + dataTable.Columns.Count - 1);
oSheet.get_Range("A1", lastColumn + "1").Value2 = colNames;
oSheet.get_Range("A1", lastColumn + "1").Font.Bold = true;
oSheet.get_Range("A1", lastColumn + "1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
DataRow[] dr = dataTable.Select();
string[,] rowData = new string[dr.Count<DataRow>(), dataTable.Columns.Count + 1];
int rowCnt = 0;
foreach (DataRow row in dr)
{
for (col = 0; col < dataTable.Columns.Count; col++)
{
rowData[rowCnt, col] = row[col].ToString();
}
rowCnt++;
}
rowCnt++;
oSheet.get_Range("A2", lastColumn + rowCnt.ToString()).Value = rowData;
oXL.Visible = false;
oXL.UserControl = true;
String sNewFolderName = "Report_" + intReportId;
filename = COMMON_FILE.SAVE_EXCEPTION_FILE_PATH + sNewFolderName + "\\" + "Exception_Person" + intReportId + "_" + DateTime.Now.ToString("dd-MM-yyyy_hh-mm-ss") + Extension;
DirectoryInfo strDirectoryInfo = new DirectoryInfo(COMMON_FILE.SAVE_EXCEPTION_FILE_PATH + sNewFolderName);
if (!strDirectoryInfo.Exists)
{
strDirectoryInfo.Create();
}
// Set "Everyone" Permission to Folder
string redirectionFolder = Convert.ToString(strDirectoryInfo);
FileSystemAccessRule everyOne = new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow);
DirectorySecurity dirSecurity = new DirectorySecurity(redirectionFolder, AccessControlSections.Group);
dirSecurity.AddAccessRule(everyOne);
Directory.SetAccessControl(redirectionFolder, dirSecurity);
oSheet.SaveAs(filename);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
oXL.Quit();
Marshal.ReleaseComObject(oSheet);
Marshal.ReleaseComObject(oWB);
Marshal.ReleaseComObject(oXL);
oSheet = null;
oWB = null;
oXL = null;
GC.GetTotalMemory(false);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.GetTotalMemory(true);
The excel is created and opened for insert value. We most close this excel using this system
Process[] localByName = Process.GetProcessesByName("EXCEL");
foreach (Process process in localByName)
{
process.Kill();
}
}
catch (Exception ex)
{
// To write log.
logFile.writeLog(ex.Message.ToString());
}
finally
{
Marshal.ReleaseComObject(oWB);
}
}
有人請幫我。 謝謝
將代碼放入try catch並打印異常,零保存路徑,與asp.net開發服務器環境相比,在IIS上下文中它可能會更改
感謝大家。
現在可以使用了。 :)
這是由於文件夾權限。
我添加了以下內容:
// Set "Everyone" Permission to Folder
string redirectionFolder = Convert.ToString(strDirectoryInfo);
FileSystemAccessRule everyOne = new FileSystemAccessRule("Everyone",
FileSystemRights.FullControl, AccessControlType.Allow); DirectorySecurity
dirSecurity = new DirectorySecurity(redirectionFolder,
AccessControlSections.Group);
dirSecurity.AddAccessRule(everyOne);
Directory.SetAccessControl(redirectionFolder, dirSecurity);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.